mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00
Add unique_id to solarlog sensors (#39186)
* Add unique_id to solarlog Add unique_id to solarlog sensors * Resolve suggested changes Resolve suggested changes * Update homeassistant/components/solarlog/sensor.py Ok, thanks for the explanation. Co-authored-by: Chris Talkington <chris@talkingtontech.com> Co-authored-by: Chris Talkington <chris@talkingtontech.com>
This commit is contained in:
parent
c3ad493bb7
commit
2e8506de80
@ -61,7 +61,7 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||||||
# Create a new sensor for each sensor type.
|
# Create a new sensor for each sensor type.
|
||||||
entities = []
|
entities = []
|
||||||
for sensor_key in SENSOR_TYPES:
|
for sensor_key in SENSOR_TYPES:
|
||||||
sensor = SolarlogSensor(platform_name, sensor_key, data)
|
sensor = SolarlogSensor(entry.entry_id, platform_name, sensor_key, data)
|
||||||
entities.append(sensor)
|
entities.append(sensor)
|
||||||
|
|
||||||
async_add_entities(entities, True)
|
async_add_entities(entities, True)
|
||||||
@ -71,20 +71,28 @@ async def async_setup_entry(hass, entry, async_add_entities):
|
|||||||
class SolarlogSensor(Entity):
|
class SolarlogSensor(Entity):
|
||||||
"""Representation of a Sensor."""
|
"""Representation of a Sensor."""
|
||||||
|
|
||||||
def __init__(self, platform_name, sensor_key, data):
|
def __init__(self, entry_id, platform_name, sensor_key, data):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
self.platform_name = platform_name
|
self.platform_name = platform_name
|
||||||
self.sensor_key = sensor_key
|
self.sensor_key = sensor_key
|
||||||
self.data = data
|
self.data = data
|
||||||
|
self.entry_id = entry_id
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
self._json_key = SENSOR_TYPES[self.sensor_key][0]
|
self._json_key = SENSOR_TYPES[self.sensor_key][0]
|
||||||
|
self._label = SENSOR_TYPES[self.sensor_key][1]
|
||||||
self._unit_of_measurement = SENSOR_TYPES[self.sensor_key][2]
|
self._unit_of_measurement = SENSOR_TYPES[self.sensor_key][2]
|
||||||
|
self._icon = SENSOR_TYPES[self.sensor_key][3]
|
||||||
|
|
||||||
|
@property
|
||||||
|
def unique_id(self):
|
||||||
|
"""Return the unique id."""
|
||||||
|
return f"{self.entry_id}_{self.sensor_key}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"""Return the name of the sensor."""
|
"""Return the name of the sensor."""
|
||||||
return "{} ({})".format(self.platform_name, SENSOR_TYPES[self.sensor_key][1])
|
return f"{self.platform_name} {self._label}"
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def unit_of_measurement(self):
|
def unit_of_measurement(self):
|
||||||
@ -94,7 +102,7 @@ class SolarlogSensor(Entity):
|
|||||||
@property
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the sensor icon."""
|
"""Return the sensor icon."""
|
||||||
return SENSOR_TYPES[self.sensor_key][3]
|
return self._icon
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user