From 65ee9cfa513639e820b25f4139c7253b23ddd0db Mon Sep 17 00:00:00 2001 From: Daniel Raper Date: Wed, 27 Dec 2023 19:30:25 +0000 Subject: [PATCH] Properly implement unique_id --- custom_components/ohme/__init__.py | 15 ++++++++++----- custom_components/ohme/binary_sensor.py | 17 +++++++++++------ custom_components/ohme/client/__init__.py | 4 ++++ custom_components/ohme/sensor.py | 15 ++++++++++----- custom_components/ohme/switch.py | 4 ++-- 5 files changed, 37 insertions(+), 18 deletions(-) diff --git a/custom_components/ohme/__init__.py b/custom_components/ohme/__init__.py index 1bf6657..9b4899b 100644 --- a/custom_components/ohme/__init__.py +++ b/custom_components/ohme/__init__.py @@ -31,6 +31,12 @@ async def async_setup_entry(hass, entry): await async_setup_dependencies(hass, config) + hass.data[DOMAIN][DATA_COORDINATOR] = OhmeUpdateCoordinator(hass=hass) + await hass.data[DOMAIN][DATA_COORDINATOR].async_config_entry_first_refresh() + + hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR] = OhmeStatisticsUpdateCoordinator(hass=hass) + await hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR].async_config_entry_first_refresh() + # Create tasks for each entity type hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, "sensor") @@ -42,10 +48,9 @@ async def async_setup_entry(hass, entry): hass.config_entries.async_forward_entry_setup(entry, "switch") ) - hass.data[DOMAIN][DATA_COORDINATOR] = OhmeUpdateCoordinator(hass=hass) - await hass.data[DOMAIN][DATA_COORDINATOR].async_config_entry_first_refresh() + return True - hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR] = OhmeStatisticsUpdateCoordinator(hass=hass) - await hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR].async_config_entry_first_refresh() +async def async_unload_entry(hass, entry): + """Unload a config entry.""" - return True + return await hass.config_entries.async_unload_platforms(entry, ['binary_sensor', 'sensor', 'switch']) diff --git a/custom_components/ohme/binary_sensor.py b/custom_components/ohme/binary_sensor.py index 87562df..758667d 100644 --- a/custom_components/ohme/binary_sensor.py +++ b/custom_components/ohme/binary_sensor.py @@ -18,10 +18,11 @@ async def async_setup_entry( async_add_entities, ): """Setup sensors and configure coordinator.""" + client = hass.data[DOMAIN][DATA_CLIENT] coordinator = hass.data[DOMAIN][DATA_COORDINATOR] - sensors = [ConnectedSensor(coordinator, hass), - ChargingSensor(coordinator, hass)] + sensors = [ConnectedSensor(coordinator, hass, client), + ChargingSensor(coordinator, hass, client)] async_add_entities(sensors, update_before_add=True) @@ -37,12 +38,14 @@ class ConnectedSensor( def __init__( self, coordinator: OhmeUpdateCoordinator, - hass: HomeAssistant): + hass: HomeAssistant, + client): super().__init__(coordinator=coordinator) self._attributes = {} self._last_updated = None self._state = False + self._client = client self.entity_id = generate_entity_id( "binary_sensor.{}", "ohme_car_connected", hass=hass) @@ -58,7 +61,7 @@ def icon(self): @property def unique_id(self) -> str: """Return the unique ID of the sensor.""" - return self.entity_id + return self._client.get_unique_id("car_connected") @property def is_on(self) -> bool: @@ -81,12 +84,14 @@ class ChargingSensor( def __init__( self, coordinator: OhmeUpdateCoordinator, - hass: HomeAssistant): + hass: HomeAssistant, + client): super().__init__(coordinator=coordinator) self._attributes = {} self._last_updated = None self._state = False + self._client = client self.entity_id = generate_entity_id( "binary_sensor.{}", "ohme_car_charging", hass=hass) @@ -102,7 +107,7 @@ def icon(self): @property def unique_id(self) -> str: """Return the unique ID of the sensor.""" - return self.entity_id + return self._client.get_unique_id("ohme_car_charging") @property def is_on(self) -> bool: diff --git a/custom_components/ohme/client/__init__.py b/custom_components/ohme/client/__init__.py index 313ec50..24d1b30 100644 --- a/custom_components/ohme/client/__init__.py +++ b/custom_components/ohme/client/__init__.py @@ -165,3 +165,7 @@ async def async_get_charge_statistics(self): def get_device_info(self): return self._device_info + + def get_unique_id(self, name): + return f"ohme_{self._serial}_{name}" + diff --git a/custom_components/ohme/sensor.py b/custom_components/ohme/sensor.py index 6ea78e0..d349bbd 100644 --- a/custom_components/ohme/sensor.py +++ b/custom_components/ohme/sensor.py @@ -19,10 +19,11 @@ async def async_setup_entry( async_add_entities ): """Setup sensors and configure coordinator.""" + client = hass.data[DOMAIN][DATA_CLIENT] coordinator = hass.data[DOMAIN][DATA_COORDINATOR] stats_coordinator = hass.data[DOMAIN][DATA_STATISTICS_COORDINATOR] - sensors = [PowerDrawSensor(coordinator, hass), EnergyUsageSensor(stats_coordinator, hass)] + sensors = [PowerDrawSensor(coordinator, hass, client), EnergyUsageSensor(stats_coordinator, hass, client)] async_add_entities(sensors, update_before_add=True) @@ -36,12 +37,14 @@ class PowerDrawSensor(CoordinatorEntity[OhmeUpdateCoordinator], SensorEntity): def __init__( self, coordinator: OhmeUpdateCoordinator, - hass: HomeAssistant): + hass: HomeAssistant, + client): super().__init__(coordinator=coordinator) self._state = None self._attributes = {} self._last_updated = None + self._client = client self.entity_id = generate_entity_id( "sensor.{}", "ohme_power_draw", hass=hass) @@ -51,7 +54,7 @@ def __init__( @property def unique_id(self) -> str: """Return the unique ID of the sensor.""" - return self.entity_id + return self._client.get_unique_id("power_draw") @property def icon(self): @@ -76,12 +79,14 @@ class EnergyUsageSensor(CoordinatorEntity[OhmeStatisticsUpdateCoordinator], Sens def __init__( self, coordinator: OhmeUpdateCoordinator, - hass: HomeAssistant): + hass: HomeAssistant, + client): super().__init__(coordinator=coordinator) self._state = None self._attributes = {} self._last_updated = None + self._client = client self.entity_id = generate_entity_id( "sensor.{}", "ohme_accumulative_energy", hass=hass) @@ -91,7 +96,7 @@ def __init__( @property def unique_id(self) -> str: """Return the unique ID of the sensor.""" - return self.entity_id + return self._client.get_unique_id("accumulative_energy") @property def icon(self): diff --git a/custom_components/ohme/switch.py b/custom_components/ohme/switch.py index 52eb244..3533f4b 100644 --- a/custom_components/ohme/switch.py +++ b/custom_components/ohme/switch.py @@ -53,7 +53,7 @@ def __init__(self, coordinator, hass: HomeAssistant, client): @property def unique_id(self): """The unique ID of the switch.""" - return self.entity_id + return self._client.get_unique_id("pause_charge") @property def icon(self): @@ -109,7 +109,7 @@ def __init__(self, coordinator, hass: HomeAssistant, client): @property def unique_id(self): """The unique ID of the switch.""" - return self.entity_id + return self._client.get_unique_id("max_charge") @property def icon(self):