Skip to content

Commit

Permalink
Properly implement unique_id
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-r committed Dec 27, 2023
1 parent fc101b0 commit 65ee9cf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 18 deletions.
15 changes: 10 additions & 5 deletions custom_components/ohme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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'])
17 changes: 11 additions & 6 deletions custom_components/ohme/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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:
Expand All @@ -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)
Expand All @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions custom_components/ohme/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"

15 changes: 10 additions & 5 deletions custom_components/ohme/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand All @@ -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):
Expand All @@ -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)
Expand All @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions custom_components/ohme/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 65ee9cf

Please sign in to comment.