Add alternative name for Tibber sensors (#26685)
* Add alternative name for Tibber sensors * refactor tibber sensorpull/26704/head
parent
a390cf7c6a
commit
72baf563fa
|
@ -44,8 +44,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||
async_add_entities(dev, True)
|
||||
|
||||
|
||||
class TibberSensorElPrice(Entity):
|
||||
"""Representation of an Tibber sensor for el price."""
|
||||
class TibberSensor(Entity):
|
||||
"""Representation of a generic Tibber sensor."""
|
||||
|
||||
def __init__(self, tibber_home):
|
||||
"""Initialize the sensor."""
|
||||
|
@ -54,10 +54,25 @@ class TibberSensorElPrice(Entity):
|
|||
self._state = None
|
||||
self._is_available = False
|
||||
self._device_state_attributes = {}
|
||||
self._unit_of_measurement = self._tibber_home.price_unit
|
||||
self._name = "Electricity price {}".format(
|
||||
tibber_home.info["viewer"]["home"]["appNickname"]
|
||||
)
|
||||
self._name = tibber_home.info["viewer"]["home"]["appNickname"]
|
||||
if self._name is None:
|
||||
self._name = tibber_home.info["viewer"]["home"]["address"].get(
|
||||
"address1", ""
|
||||
)
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return self._device_state_attributes
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
return self._state
|
||||
|
||||
|
||||
class TibberSensorElPrice(TibberSensor):
|
||||
"""Representation of a Tibber sensor for el price."""
|
||||
|
||||
async def async_update(self):
|
||||
"""Get the latest data and updates the states."""
|
||||
|
@ -86,11 +101,6 @@ class TibberSensorElPrice(Entity):
|
|||
self._device_state_attributes.update(attrs)
|
||||
self._is_available = self._state is not None
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return self._device_state_attributes
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
|
@ -99,12 +109,7 @@ class TibberSensorElPrice(Entity):
|
|||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
return self._state
|
||||
return "Electricity price {}".format(self._name)
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
|
@ -114,7 +119,7 @@ class TibberSensorElPrice(Entity):
|
|||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement of this entity."""
|
||||
return self._unit_of_measurement
|
||||
return self._tibber_home.price_unit
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
|
@ -139,17 +144,8 @@ class TibberSensorElPrice(Entity):
|
|||
]["estimatedAnnualConsumption"]
|
||||
|
||||
|
||||
class TibberSensorRT(Entity):
|
||||
"""Representation of an Tibber sensor for real time consumption."""
|
||||
|
||||
def __init__(self, tibber_home):
|
||||
"""Initialize the sensor."""
|
||||
self._tibber_home = tibber_home
|
||||
self._state = None
|
||||
self._device_state_attributes = {}
|
||||
self._unit_of_measurement = "W"
|
||||
nickname = tibber_home.info["viewer"]["home"]["appNickname"]
|
||||
self._name = f"Real time consumption {nickname}"
|
||||
class TibberSensorRT(TibberSensor):
|
||||
"""Representation of a Tibber sensor for real time consumption."""
|
||||
|
||||
async def async_added_to_hass(self):
|
||||
"""Start unavailability tracking."""
|
||||
|
@ -175,11 +171,6 @@ class TibberSensorRT(Entity):
|
|||
|
||||
self.async_schedule_update_ha_state()
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
"""Return the state attributes."""
|
||||
return self._device_state_attributes
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return True if entity is available."""
|
||||
|
@ -188,18 +179,13 @@ class TibberSensorRT(Entity):
|
|||
@property
|
||||
def name(self):
|
||||
"""Return the name of the sensor."""
|
||||
return self._name
|
||||
return "Real time consumption {}".format(self._name)
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return the polling state."""
|
||||
return False
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""Return the state of the device."""
|
||||
return self._state
|
||||
|
||||
@property
|
||||
def icon(self):
|
||||
"""Return the icon to use in the frontend."""
|
||||
|
@ -208,7 +194,7 @@ class TibberSensorRT(Entity):
|
|||
@property
|
||||
def unit_of_measurement(self):
|
||||
"""Return the unit of measurement of this entity."""
|
||||
return self._unit_of_measurement
|
||||
return "W"
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
|
|
Loading…
Reference in New Issue