Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ When using combinations of configuration methods, the order of precedence is as
| --saic-relogin-delay | SAIC_RELOGIN_DELAY | The gateway detects logins from other devices (e.g. the iSMART app). It then pauses it's activity for 900 seconds (default value). The delay can be configured with this parameter. |
| --messages-request-interval | MESSAGES_REQUEST_INTERVAL | The interval for retrieving messages in seconds. Default is 60 seconds. |
| --battery-capacity-mapping | BATTERY_CAPACITY_MAPPING | Mapping of VIN to full battery capacity. Multiple mappings can be provided separated by ',' Example: LSJXXXX=54.0,LSJYYYY=64.0 |
| --power-tailgate-vins | POWER_TAILGATE_VINS | VINs of the cars with a power tailgate, separated by ','. Enables `/doors/boot/powerTailgate/set` and the Home Assistant "Power Tailgate" cover for these cars only. Example: LSJXXXX,LSJYYYY |
| --charge-min-percentage | CHARGE_MIN_PERCENTAGE | How many % points we should try to refresh the charge state. 1.0 by default |
| --account-refresh-interval | ACCOUNT_REFRESH_INTERVAL | Interval in seconds for refreshing account-level data (vehicle list, timezone). Default is 86400 (24 hours). |
| --saic-user-timezone | SAIC_USER_TIMEZONE | Force the account timezone instead of trusting the SAIC API value. Accepts an IANA name (e.g. `Australia/Sydney`) or `GMT+HH:MM`. Mismatches with the API offset are logged. |
Expand Down Expand Up @@ -221,7 +220,7 @@ with the default vehicle prefix: `saic/<saic_user>/vehicles/<vehicle_id>`
| /drivetrain/batteryHeating/set | true/false | Start (true) or stop (false) battery heating. The car may refuse the command if it is deemed unnecessary |
| /drivetrain/batteryHeatingSchedule/set | JSON Payload. Fields are startTime and mode | Set the battery heating schedule. Mode can be either on or off |
| /doors/boot/set | true/false | Lock or unlock boot |
| /doors/boot/powerTailgate/set | open/close | Open or close the power tailgate. Only for cars listed in `POWER_TAILGATE_VINS`. Tested on an MG S6 EV only; cars without a power tailgate are expected to refuse it. The state is reported on `/doors/boot`. |
| /doors/boot/powerTailgate/set | open/close | Open or close the power tailgate. Sent to every car; the Home Assistant cover is disabled by default. Tested on an MG S6 EV only; cars without a power tailgate are expected to refuse it. The state is reported on `/doors/boot`. |
| /doors/locked/set | true/false | Lock or unlock your car. This is not always working. It might take some time until it takes effect. Don't trust this feature. Use your car key! |
| /climate/remoteTemperature/set | temperature | Set A/C temperature |
| /climate/remoteClimateState/set | on/off/front/blowingonly | Turn A/C on or off, activate A/C blowing (front) or blowing only (blowingonly) |
Expand Down
1 change: 0 additions & 1 deletion src/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ def __init__(self) -> None:
self.saic_read_timeout: float = 10.0 # in seconds
self.saic_user_timezone: ZoneInfo | None = None
self.battery_capacity_map: dict[str, float] = {}
self.power_tailgate_vins: set[str] = set()
self.mqtt_host: str | None = None
self.mqtt_port: int = 1883
self.mqtt_transport_protocol: TransportProtocol = TransportProtocol.TCP
Expand Down
16 changes: 0 additions & 16 deletions src/configuration/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ def __setup_gateway_features(args: Namespace, config: Configuration) -> None:
config.battery_capacity_map,
value_type=check_positive_float,
)
if args.power_tailgate_vins:
config.power_tailgate_vins = {
vin.strip() for vin in args.power_tailgate_vins.split(",") if vin.strip()
}
try:
config.messages_request_interval = int(args.messages_request_interval)
except ValueError as ve:
Expand Down Expand Up @@ -385,18 +381,6 @@ def __add_saic_api_argument_group(
envvar="BATTERY_CAPACITY_MAPPING",
type=str,
)
saic_api.add_argument(
"--power-tailgate-vins",
help="""VINs of the cars with a power tailgate, separated by comma.
Enables the doors/boot/powerTailgate/set command and its
Home Assistant cover for these cars only.
Example: LSJXXXX,LSJYYYY""",
dest="power_tailgate_vins",
required=False,
action=EnvDefault,
envvar="POWER_TAILGATE_VINS",
type=str,
)
saic_api.add_argument(
"--saic-relogin-delay",
help="""How long to wait before attempting another login to the SAIC API.""",
Expand Down
9 changes: 2 additions & 7 deletions src/handlers/command/doors/doors_power_tailgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from saic_ismart_client_ng.api.vehicle.locks import TailgateAction

from exceptions import MqttGatewayException
from handlers.command.base import (
RESULT_REFRESH_AND_CLEAR,
CommandProcessingResult,
Expand Down Expand Up @@ -33,12 +32,8 @@ def options(self) -> dict[str, Callable[[], Awaitable[None]]]:
}

async def __control_tailgate(self, action: TailgateAction) -> None:
if not self.vehicle_state.vehicle.has_power_tailgate:
msg = (
f"Power tailgate is not enabled for vehicle {self.vin}. "
"Add its VIN to POWER_TAILGATE_VINS to enable it"
)
raise MqttGatewayException(msg)
# Sent to every car: nothing in the vehicle data tells which cars have
# a power tailgate. Cars without one are expected to refuse it.
LOG.info(f"Vehicle {self.vin} power tailgate will {action.name.lower()}")
await self.saic_api.control_tailgate(self.vin, action=action)

Expand Down
21 changes: 11 additions & 10 deletions src/integrations/home_assistant/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -680,16 +680,17 @@ def __publish_doors_sensors(self) -> None:
)

def __publish_power_tailgate(self) -> None:
if self.__vin_info.has_power_tailgate:
self._publish_cover(
mqtt_topics.DOORS_BOOT,
mqtt_topics.DOORS_BOOT_POWER_TAILGATE,
"Power Tailgate",
# No static icon: HA then shows an open or closed door icon
device_class="door",
)
else:
self.__unpublish_ha_discovery_message("cover", "Power Tailgate")
# Nothing in the vehicle data tells which cars have a power tailgate,
# so the cover is offered to every car but disabled by default. Cars
# without one are expected to refuse the command.
self._publish_cover(
mqtt_topics.DOORS_BOOT,
mqtt_topics.DOORS_BOOT_POWER_TAILGATE,
"Power Tailgate",
enabled=False,
# No static icon: HA then shows an open or closed door icon
device_class="door",
)

def __publish_gateway_sensors(self) -> None:
options = [m.value for m in RefreshMode]
Expand Down
6 changes: 1 addition & 5 deletions src/mqtt_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,7 @@ def __create_vehicle_handler(self, vin_info: VinInfo) -> VehicleHandler:
total_battery_capacity = (
self.configuration.battery_capacity_map.get(vin, None) if vin else None
)
info = VehicleInfo(
vin_info,
total_battery_capacity,
has_power_tailgate=vin in self.configuration.power_tailgate_vins,
)
info = VehicleInfo(vin_info, total_battery_capacity)
account_prefix = f"{self.configuration.saic_user}/{mqtt_topics.VEHICLES}/{vin}"
vehicle_state = VehicleState(
self.publisher,
Expand Down
9 changes: 0 additions & 9 deletions src/vehicle_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ def __init__(
self,
vin_info: VinInfo,
custom_battery_capacity: float | None,
*,
has_power_tailgate: bool = False,
) -> None:
if not vin_info.vin:
raise MqttGatewayException("Could not handle a car without a vin")
Expand All @@ -34,13 +32,6 @@ def __init__(
self.__properties_from_configuration(self.configuration)
)
self.__custom_battery_capacity: float | None = custom_battery_capacity
# No model configuration flag tells us if the car has a power tailgate,
# so the user opts in per VIN (POWER_TAILGATE_VINS).
self.__has_power_tailgate: Final[bool] = has_power_tailgate

@property
def has_power_tailgate(self) -> bool:
return self.__has_power_tailgate

@property
def custom_battery_capacity(self) -> float | None:
Expand Down
24 changes: 7 additions & 17 deletions tests/handlers/command/test_doors_power_tailgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
from vehicle_info import VehicleInfo


def _make_vehicle_state(*, has_power_tailgate: bool) -> MagicMock:
def _make_vehicle_state() -> MagicMock:
vin_info = VinInfo()
vin_info.vin = "vin_test_000000000"
vehicle_info = VehicleInfo(vin_info, None, has_power_tailgate=has_power_tailgate)
vehicle_info = VehicleInfo(vin_info, None)

vehicle_state = MagicMock()
vehicle_state.vehicle = vehicle_info
Expand All @@ -36,7 +36,7 @@ def test_topic(self) -> None:
)

async def test_open(self) -> None:
vehicle_state = _make_vehicle_state(has_power_tailgate=True)
vehicle_state = _make_vehicle_state()
saic_api = AsyncMock()
handler = DoorsPowerTailgateCommand(saic_api, vehicle_state)

Expand All @@ -48,7 +48,7 @@ async def test_open(self) -> None:
)

async def test_close(self) -> None:
vehicle_state = _make_vehicle_state(has_power_tailgate=True)
vehicle_state = _make_vehicle_state()
saic_api = AsyncMock()
handler = DoorsPowerTailgateCommand(saic_api, vehicle_state)

Expand All @@ -59,18 +59,8 @@ async def test_close(self) -> None:
vehicle_state.vin, action=TailgateAction.CLOSE
)

async def test_reject_when_not_enabled(self) -> None:
vehicle_state = _make_vehicle_state(has_power_tailgate=False)
saic_api = AsyncMock()
handler = DoorsPowerTailgateCommand(saic_api, vehicle_state)

with pytest.raises(MqttGatewayException, match="POWER_TAILGATE_VINS"):
await handler.handle("open")

saic_api.control_tailgate.assert_not_called()

async def test_reject_unknown_payload(self) -> None:
vehicle_state = _make_vehicle_state(has_power_tailgate=True)
vehicle_state = _make_vehicle_state()
saic_api = AsyncMock()
handler = DoorsPowerTailgateCommand(saic_api, vehicle_state)

Expand All @@ -80,7 +70,7 @@ async def test_reject_unknown_payload(self) -> None:
saic_api.control_tailgate.assert_not_called()

async def test_empty_payload_does_nothing(self) -> None:
vehicle_state = _make_vehicle_state(has_power_tailgate=True)
vehicle_state = _make_vehicle_state()
saic_api = AsyncMock()
handler = DoorsPowerTailgateCommand(saic_api, vehicle_state)

Expand All @@ -92,7 +82,7 @@ async def test_empty_payload_does_nothing(self) -> None:

class TestDoorsBootCommandUnchanged(unittest.IsolatedAsyncioTestCase):
async def test_false_still_unlocks_the_latch(self) -> None:
vehicle_state = _make_vehicle_state(has_power_tailgate=True)
vehicle_state = _make_vehicle_state()
saic_api = AsyncMock()
handler = DoorsBootCommand(saic_api, vehicle_state)

Expand Down
20 changes: 7 additions & 13 deletions tests/integrations/home_assistant/test_discovery_power_tailgate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
COVER_TOPIC = f"homeassistant/cover/{VIN}_mg/{VIN}_power_tailgate/config"


def _publish_discovery(*, has_power_tailgate: bool) -> MessageCapturingConsolePublisher:
def _publish_discovery() -> MessageCapturingConsolePublisher:
config = Configuration()
config.anonymized_publishing = False
config.ha_discovery_prefix = "homeassistant"
Expand All @@ -25,7 +25,7 @@ def _publish_discovery(*, has_power_tailgate: bool) -> MessageCapturingConsolePu
vin_info.series = "EH32 S"
vin_info.modelName = "MG4 Electric"
vin_info.modelYear = "2022"
vehicle_info = VehicleInfo(vin_info, None, has_power_tailgate=has_power_tailgate)
vehicle_info = VehicleInfo(vin_info, None)
vehicle_state = VehicleState(
publisher, BlockingScheduler(), f"/vehicles/{VIN}", vehicle_info
)
Expand All @@ -40,8 +40,8 @@ def _publish_discovery(*, has_power_tailgate: bool) -> MessageCapturingConsolePu
return publisher


def test_power_tailgate_cover_published_when_enabled() -> None:
publisher = _publish_discovery(has_power_tailgate=True)
def test_power_tailgate_cover_published_for_every_car() -> None:
publisher = _publish_discovery()

assert COVER_TOPIC in publisher.map
payload = json.loads(publisher.map[COVER_TOPIC])
Expand All @@ -54,17 +54,11 @@ def test_power_tailgate_cover_published_when_enabled() -> None:
assert payload["state_open"] == "True"
assert payload["state_closed"] == "False"
assert payload["optimistic"] is False
assert payload["enabled_by_default"] is False


def test_power_tailgate_cover_unpublished_when_disabled() -> None:
publisher = _publish_discovery(has_power_tailgate=False)

assert COVER_TOPIC in publisher.map
assert publisher.map[COVER_TOPIC] == ""


def test_boot_lock_unchanged_when_power_tailgate_enabled() -> None:
publisher = _publish_discovery(has_power_tailgate=True)
def test_boot_lock_unchanged() -> None:
publisher = _publish_discovery()

lock_topic = f"homeassistant/lock/{VIN}_mg/{VIN}_boot_lock/config"
payload = json.loads(publisher.map[lock_topic])
Expand Down
31 changes: 0 additions & 31 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,34 +131,3 @@ def test_process_command_line_rejects_unknown_preset(
)
with pytest.raises(SystemExit):
process_command_line()


def test_process_command_line_sets_power_tailgate_vins(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.setattr(
"sys.argv",
[
"prog",
"--saic-user",
"[email protected]",
"--saic-password",
"secret",
"--power-tailgate-vins",
"LSJAAAA, LSJBBBB,",
],
)
config = process_command_line()
assert config.power_tailgate_vins == {"LSJAAAA", "LSJBBBB"}


def test_process_command_line_defaults_power_tailgate_vins_to_empty(
monkeypatch: pytest.MonkeyPatch,
) -> None:
monkeypatch.delenv("POWER_TAILGATE_VINS", raising=False)
monkeypatch.setattr(
"sys.argv",
["prog", "--saic-user", "[email protected]", "--saic-password", "secret"],
)
config = process_command_line()
assert config.power_tailgate_vins == set()
Loading