diff --git a/README.md b/README.md index de3d343..eca80bd 100644 --- a/README.md +++ b/README.md @@ -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. | @@ -221,7 +220,7 @@ with the default vehicle prefix: `saic//vehicles/` | /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) | diff --git a/src/configuration/__init__.py b/src/configuration/__init__.py index a4b22cf..4b29149 100644 --- a/src/configuration/__init__.py +++ b/src/configuration/__init__.py @@ -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 diff --git a/src/configuration/parser.py b/src/configuration/parser.py index 467ad88..f33e9ea 100644 --- a/src/configuration/parser.py +++ b/src/configuration/parser.py @@ -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: @@ -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.""", diff --git a/src/handlers/command/doors/doors_power_tailgate.py b/src/handlers/command/doors/doors_power_tailgate.py index 4de33c2..f2c64e3 100644 --- a/src/handlers/command/doors/doors_power_tailgate.py +++ b/src/handlers/command/doors/doors_power_tailgate.py @@ -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, @@ -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) diff --git a/src/integrations/home_assistant/discovery.py b/src/integrations/home_assistant/discovery.py index 6c3bcfa..cd85c28 100644 --- a/src/integrations/home_assistant/discovery.py +++ b/src/integrations/home_assistant/discovery.py @@ -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] diff --git a/src/mqtt_gateway.py b/src/mqtt_gateway.py index 56e2b18..8db7343 100644 --- a/src/mqtt_gateway.py +++ b/src/mqtt_gateway.py @@ -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, diff --git a/src/vehicle_info.py b/src/vehicle_info.py index db62e15..e98ffbb 100644 --- a/src/vehicle_info.py +++ b/src/vehicle_info.py @@ -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") @@ -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: diff --git a/tests/handlers/command/test_doors_power_tailgate.py b/tests/handlers/command/test_doors_power_tailgate.py index cddebac..0c9505f 100644 --- a/tests/handlers/command/test_doors_power_tailgate.py +++ b/tests/handlers/command/test_doors_power_tailgate.py @@ -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 @@ -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) @@ -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) @@ -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) @@ -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) @@ -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) diff --git a/tests/integrations/home_assistant/test_discovery_power_tailgate.py b/tests/integrations/home_assistant/test_discovery_power_tailgate.py index 66c7123..10993ce 100644 --- a/tests/integrations/home_assistant/test_discovery_power_tailgate.py +++ b/tests/integrations/home_assistant/test_discovery_power_tailgate.py @@ -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" @@ -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 ) @@ -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]) @@ -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]) diff --git a/tests/test_parser.py b/tests/test_parser.py index 04dbf90..a03278a 100644 --- a/tests/test_parser.py +++ b/tests/test_parser.py @@ -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", - "user@example.com", - "--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", "user@example.com", "--saic-password", "secret"], - ) - config = process_command_line() - assert config.power_tailgate_vins == set()