From 5e973dd0172f8ddcc640f41d8ad1e68c0e303919 Mon Sep 17 00:00:00 2001 From: fredericvl <34839323+fredericvl@users.noreply.github.com> Date: Fri, 15 Nov 2019 09:21:46 +0100 Subject: [PATCH] Change unique id for SAJ sensor based on device SN (#28663) * Change unique id for SAJ sensor based on device SN * Add SAJ device name + sn to state attributes * Revert device state attributes (after review) --- homeassistant/components/saj/manifest.json | 2 +- homeassistant/components/saj/sensor.py | 21 +++++++++++---------- requirements_all.txt | 2 +- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/saj/manifest.json b/homeassistant/components/saj/manifest.json index 4d02ab74840..02d83916d50 100644 --- a/homeassistant/components/saj/manifest.json +++ b/homeassistant/components/saj/manifest.json @@ -3,7 +3,7 @@ "name": "SAJ", "documentation": "https://www.home-assistant.io/integrations/saj", "requirements": [ - "pysaj==0.0.13" + "pysaj==0.0.14" ], "dependencies": [], "codeowners": [ diff --git a/homeassistant/components/saj/sensor.py b/homeassistant/components/saj/sensor.py index 7542440c102..2a17d110c6e 100644 --- a/homeassistant/components/saj/sensor.py +++ b/homeassistant/components/saj/sensor.py @@ -69,9 +69,6 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= # Use all sensors by default hass_sensors = [] - for sensor in sensor_def: - hass_sensors.append(SAJsensor(sensor, inverter_name=config.get(CONF_NAME))) - kwargs = {} if wifi: kwargs["wifi"] = True @@ -81,7 +78,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= try: saj = pysaj.SAJ(config[CONF_HOST], **kwargs) - await saj.read(sensor_def) + done = await saj.read(sensor_def) except pysaj.UnauthorizedException: _LOGGER.error("Username and/or password is wrong.") return @@ -91,7 +88,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= ) return - async_add_entities(hass_sensors) + if done: + for sensor in sensor_def: + hass_sensors.append( + SAJsensor(saj.serialnumber, sensor, inverter_name=config.get(CONF_NAME)) + ) + + async_add_entities(hass_sensors) async def async_saj(): """Update all the SAJ sensors.""" @@ -163,10 +166,11 @@ def async_track_time_interval_backoff(hass, action) -> CALLBACK_TYPE: class SAJsensor(Entity): """Representation of a SAJ sensor.""" - def __init__(self, pysaj_sensor, inverter_name=None): + def __init__(self, serialnumber, pysaj_sensor, inverter_name=None): """Initialize the sensor.""" self._sensor = pysaj_sensor self._inverter_name = inverter_name + self._serialnumber = serialnumber self._state = self._sensor.value @property @@ -235,7 +239,4 @@ class SAJsensor(Entity): @property def unique_id(self): """Return a unique identifier for this sensor.""" - if self._inverter_name: - return f"{self._inverter_name}_{self._sensor.name}" - - return f"{self._sensor.name}" + return f"{self._serialnumber}_{self._sensor.name}" diff --git a/requirements_all.txt b/requirements_all.txt index c91b6050af3..5977b1fef0e 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1435,7 +1435,7 @@ pyrepetier==3.0.5 pysabnzbd==1.1.0 # homeassistant.components.saj -pysaj==0.0.13 +pysaj==0.0.14 # homeassistant.components.sony_projector pysdcp==1