From 788813aad6dab3686c3bb433265dbc3ce2b9412c Mon Sep 17 00:00:00 2001 From: Duco Sebel <74970928+DCSBL@users.noreply.github.com> Date: Tue, 26 Mar 2024 10:34:36 +0100 Subject: [PATCH] Implement Identify button and Cloud connection toggle for HomeWizard Watermeter (#113814) --- .../components/homewizard/coordinator.py | 12 +-- .../components/homewizard/manifest.json | 2 +- homeassistant/components/homewizard/switch.py | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- .../homewizard/fixtures/HWE-WTR/system.json | 3 + .../snapshots/test_diagnostics.ambr | 4 +- .../homewizard/snapshots/test_switch.ambr | 80 +++++++++++++++++++ tests/components/homewizard/test_button.py | 4 +- tests/components/homewizard/test_switch.py | 2 +- 10 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 tests/components/homewizard/fixtures/HWE-WTR/system.json diff --git a/homeassistant/components/homewizard/coordinator.py b/homeassistant/components/homewizard/coordinator.py index 22133a04234..db41d1dd128 100644 --- a/homeassistant/components/homewizard/coordinator.py +++ b/homeassistant/components/homewizard/coordinator.py @@ -5,7 +5,7 @@ from __future__ import annotations import logging from homewizard_energy import HomeWizardEnergy -from homewizard_energy.const import SUPPORTS_IDENTIFY, SUPPORTS_STATE, SUPPORTS_SYSTEM +from homewizard_energy.const import SUPPORTS_IDENTIFY, SUPPORTS_STATE from homewizard_energy.errors import DisabledError, RequestError, UnsupportedError from homewizard_energy.models import Device @@ -53,8 +53,7 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry] if self.supports_state(data.device): data.state = await self.api.state() - if self.supports_system(data.device): - data.system = await self.api.system() + data.system = await self.api.system() except UnsupportedError as ex: # Old firmware, ignore @@ -94,13 +93,6 @@ class HWEnergyDeviceUpdateCoordinator(DataUpdateCoordinator[DeviceResponseEntry] return device.product_type in SUPPORTS_STATE - def supports_system(self, device: Device | None = None) -> bool: - """Return True if the device supports system.""" - if device is None: - device = self.data.device - - return device.product_type in SUPPORTS_SYSTEM - def supports_identify(self, device: Device | None = None) -> bool: """Return True if the device supports identify.""" if device is None: diff --git a/homeassistant/components/homewizard/manifest.json b/homeassistant/components/homewizard/manifest.json index 149d5b891f4..7355d9405df 100644 --- a/homeassistant/components/homewizard/manifest.json +++ b/homeassistant/components/homewizard/manifest.json @@ -7,6 +7,6 @@ "iot_class": "local_polling", "loggers": ["homewizard_energy"], "quality_scale": "platinum", - "requirements": ["python-homewizard-energy==4.3.1"], + "requirements": ["python-homewizard-energy==v5.0.0"], "zeroconf": ["_hwenergy._tcp.local."] } diff --git a/homeassistant/components/homewizard/switch.py b/homeassistant/components/homewizard/switch.py index eb954709be0..299eb9e806b 100644 --- a/homeassistant/components/homewizard/switch.py +++ b/homeassistant/components/homewizard/switch.py @@ -57,7 +57,7 @@ SWITCHES = [ key="cloud_connection", translation_key="cloud_connection", entity_category=EntityCategory.CONFIG, - create_fn=lambda coordinator: coordinator.supports_system(), + create_fn=lambda _: True, available_fn=lambda data: data.system is not None, is_on_fn=lambda data: data.system.cloud_enabled if data.system else None, set_fn=lambda api, active: api.system_set(cloud_enabled=active), diff --git a/requirements_all.txt b/requirements_all.txt index b8eeaebd953..dc3c262f3dd 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2229,7 +2229,7 @@ python-gitlab==1.6.0 python-homeassistant-analytics==0.6.0 # homeassistant.components.homewizard -python-homewizard-energy==4.3.1 +python-homewizard-energy==v5.0.0 # homeassistant.components.hp_ilo python-hpilo==4.4.3 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 43a92f69d36..eb7b2162cc5 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1720,7 +1720,7 @@ python-fullykiosk==0.0.12 python-homeassistant-analytics==0.6.0 # homeassistant.components.homewizard -python-homewizard-energy==4.3.1 +python-homewizard-energy==v5.0.0 # homeassistant.components.izone python-izone==1.2.9 diff --git a/tests/components/homewizard/fixtures/HWE-WTR/system.json b/tests/components/homewizard/fixtures/HWE-WTR/system.json new file mode 100644 index 00000000000..362491b3519 --- /dev/null +++ b/tests/components/homewizard/fixtures/HWE-WTR/system.json @@ -0,0 +1,3 @@ +{ + "cloud_enabled": true +} diff --git a/tests/components/homewizard/snapshots/test_diagnostics.ambr b/tests/components/homewizard/snapshots/test_diagnostics.ambr index f42c2abfc95..ed744083373 100644 --- a/tests/components/homewizard/snapshots/test_diagnostics.ambr +++ b/tests/components/homewizard/snapshots/test_diagnostics.ambr @@ -544,7 +544,9 @@ 'serial': '**REDACTED**', }), 'state': None, - 'system': None, + 'system': dict({ + 'cloud_enabled': True, + }), }), 'entry': dict({ 'ip_address': '**REDACTED**', diff --git a/tests/components/homewizard/snapshots/test_switch.ambr b/tests/components/homewizard/snapshots/test_switch.ambr index 8877fe01a91..99a5bcab6cb 100644 --- a/tests/components/homewizard/snapshots/test_switch.ambr +++ b/tests/components/homewizard/snapshots/test_switch.ambr @@ -641,6 +641,86 @@ 'via_device_id': None, }) # --- +# name: test_switch_entities[HWE-WTR-switch.device_cloud_connection-system_set-cloud_enabled] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Device Cloud connection', + }), + 'context': , + 'entity_id': 'switch.device_cloud_connection', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'on', + }) +# --- +# name: test_switch_entities[HWE-WTR-switch.device_cloud_connection-system_set-cloud_enabled].1 + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'switch', + 'entity_category': , + 'entity_id': 'switch.device_cloud_connection', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Cloud connection', + 'platform': 'homewizard', + 'previous_unique_id': None, + 'supported_features': 0, + 'translation_key': 'cloud_connection', + 'unique_id': 'aabbccddeeff_cloud_connection', + 'unit_of_measurement': None, + }) +# --- +# name: test_switch_entities[HWE-WTR-switch.device_cloud_connection-system_set-cloud_enabled].2 + DeviceRegistryEntrySnapshot({ + 'area_id': None, + 'config_entries': , + 'configuration_url': None, + 'connections': set({ + tuple( + 'mac', + '3c:39:e7:aa:bb:cc', + ), + }), + 'disabled_by': None, + 'entry_type': None, + 'hw_version': None, + 'id': , + 'identifiers': set({ + tuple( + 'homewizard', + '3c39e7aabbcc', + ), + }), + 'is_new': False, + 'labels': set({ + }), + 'manufacturer': 'HomeWizard', + 'model': 'HWE-WTR', + 'name': 'Device', + 'name_by_user': None, + 'serial_number': None, + 'suggested_area': None, + 'sw_version': '2.03', + 'via_device_id': None, + }) +# --- # name: test_switch_entities[SDM230-switch.device_cloud_connection-system_set-cloud_enabled] StateSnapshot({ 'attributes': ReadOnlyDict({ diff --git a/tests/components/homewizard/test_button.py b/tests/components/homewizard/test_button.py index 7f7ada37644..928e6f21901 100644 --- a/tests/components/homewizard/test_button.py +++ b/tests/components/homewizard/test_button.py @@ -18,9 +18,7 @@ pytestmark = [ ] -@pytest.mark.parametrize( - "device_fixture", ["HWE-WTR", "SDM230", "SDM630", "HWE-KWH1", "HWE-KWH3"] -) +@pytest.mark.parametrize("device_fixture", ["SDM230", "SDM630", "HWE-KWH1", "HWE-KWH3"]) async def test_identify_button_entity_not_loaded_when_not_available( hass: HomeAssistant, ) -> None: diff --git a/tests/components/homewizard/test_switch.py b/tests/components/homewizard/test_switch.py index e9d036a01d8..b9e812620e8 100644 --- a/tests/components/homewizard/test_switch.py +++ b/tests/components/homewizard/test_switch.py @@ -42,7 +42,6 @@ pytestmark = [ [ "switch.device", "switch.device_switch_lock", - "switch.device_cloud_connection", ], ), ( @@ -93,6 +92,7 @@ async def test_entities_not_created_for_device( ("HWE-SKT-21", "switch.device", "state_set", "power_on"), ("HWE-SKT-21", "switch.device_switch_lock", "state_set", "switch_lock"), ("HWE-SKT-21", "switch.device_cloud_connection", "system_set", "cloud_enabled"), + ("HWE-WTR", "switch.device_cloud_connection", "system_set", "cloud_enabled"), ("SDM230", "switch.device_cloud_connection", "system_set", "cloud_enabled"), ("SDM630", "switch.device_cloud_connection", "system_set", "cloud_enabled"), ("HWE-KWH1", "switch.device_cloud_connection", "system_set", "cloud_enabled"),