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)
This commit is contained in:
fredericvl 2019-11-15 09:21:46 +01:00 committed by Martin Hjelmare
parent bc14e93ae3
commit 5e973dd017
3 changed files with 13 additions and 12 deletions

View File

@ -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": [

View File

@ -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}"

View File

@ -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