Skip to content

Commit cfed0cd

Browse files
✨ Expose detected address as diagnostic sensor
This will help to see data sent to Enedis. Change-Id: I1c990d14d2fa10069ebf70612b2e21c3d5398bf2
1 parent b57b5ef commit cfed0cd

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

custom_components/rte_ecowatt/__init__.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ async def update_method(self):
593593
shedding_event["refresh_date"] = datetime.strptime(
594594
shedding_event["refresh_date"], "%Y-%m-%dT%H:%M:%S%z"
595595
)
596+
data["address"] = { "street": street, "insee_code": city_code }
596597
return data
597598
except Exception as err:
598599
raise UpdateFailed(f"Error communicating with API: {err}")
@@ -728,3 +729,43 @@ async def async_migrate_entry(hass, config_entry: ConfigEntry):
728729
hass.config_entries.async_update_entry(config_entry, data=new)
729730
_LOGGER.info(f"Migration to version {config_entry.version} successful")
730731
return True
732+
733+
class DetectedAddress(CoordinatorEntity, RestorableCoordinatedSensor):
734+
"""Exposes the address detected from GPS coordinate and sent to Enedis"""
735+
736+
def __init__(
737+
self, coordinator: EnedisAPICoordinator, hass: HomeAssistant
738+
):
739+
super().__init__(coordinator)
740+
self._restored = False
741+
self.hass = hass
742+
self._attr_extra_state_attributes: Dict[str, Any] = {}
743+
self._attr_name = "Detected address"
744+
self._state = None
745+
self._attr_entity_category = EntityCategory.DIAGNOSTIC
746+
747+
@callback
748+
def _handle_coordinator_update(self) -> None:
749+
if not self.coordinator.last_update_success:
750+
_LOGGER.debug("Last coordinator failed, assuming state has not changed")
751+
return
752+
data = self.coordinator.data['address']
753+
self._state = f"{data['street']} {data['insee_code']}"
754+
self.async_write_ha_state()
755+
756+
@property
757+
def state(self) -> Optional[str]:
758+
return self._state
759+
760+
@property
761+
def native_value(self):
762+
return self._state
763+
764+
@property
765+
def device_info(self):
766+
# technically the sensor should belong to a French Governement entity
767+
return {"identifiers": {(DOMAIN, "enedis")}, "name": "Enedis"}
768+
769+
@property
770+
def unique_id(self) -> str:
771+
return f"enedis-sent-address"

custom_components/rte_ecowatt/sensor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414
from homeassistant.config_entries import ConfigEntry
1515

1616
from . import (
17-
EcoWattAPICoordinator,
1817
HourlyEcowattLevel,
1918
DailyEcowattLevel,
20-
EnedisAPICoordinator,
2119
ElectricityDistributorEntity,
20+
DetectedAddress,
2221
)
2322
from .const import (
2423
DOMAIN,
@@ -74,6 +73,7 @@ async def async_setup_entry(
7473
0
7574
]: # this sensor transmit PII to external provider, it's opt-in
7675
sensors.append(ElectricityDistributorEntity(enedis_coordinator, hass))
76+
sensors.append(DetectedAddress(enedis_coordinator, hass))
7777

7878
async_add_entities(sensors)
7979
while not all(s.restored for s in sensors):

0 commit comments

Comments
 (0)