Add alternative name for Tibber sensors (#26685)

* Add alternative name for Tibber sensors

* refactor tibber sensor
This commit is contained in:
Daniel Høyer Iversen 2019-09-18 08:30:59 +03:00 committed by GitHub
parent a390cf7c6a
commit 72baf563fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -44,8 +44,8 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async_add_entities(dev, True) async_add_entities(dev, True)
class TibberSensorElPrice(Entity): class TibberSensor(Entity):
"""Representation of an Tibber sensor for el price.""" """Representation of a generic Tibber sensor."""
def __init__(self, tibber_home): def __init__(self, tibber_home):
"""Initialize the sensor.""" """Initialize the sensor."""
@ -54,11 +54,26 @@ class TibberSensorElPrice(Entity):
self._state = None self._state = None
self._is_available = False self._is_available = False
self._device_state_attributes = {} self._device_state_attributes = {}
self._unit_of_measurement = self._tibber_home.price_unit self._name = tibber_home.info["viewer"]["home"]["appNickname"]
self._name = "Electricity price {}".format( if self._name is None:
tibber_home.info["viewer"]["home"]["appNickname"] 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): async def async_update(self):
"""Get the latest data and updates the states.""" """Get the latest data and updates the states."""
now = dt_util.now() now = dt_util.now()
@ -86,11 +101,6 @@ class TibberSensorElPrice(Entity):
self._device_state_attributes.update(attrs) self._device_state_attributes.update(attrs)
self._is_available = self._state is not None self._is_available = self._state is not None
@property
def device_state_attributes(self):
"""Return the state attributes."""
return self._device_state_attributes
@property @property
def available(self): def available(self):
"""Return True if entity is available.""" """Return True if entity is available."""
@ -99,12 +109,7 @@ class TibberSensorElPrice(Entity):
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""
return self._name return "Electricity price {}".format(self._name)
@property
def state(self):
"""Return the state of the device."""
return self._state
@property @property
def icon(self): def icon(self):
@ -114,7 +119,7 @@ class TibberSensorElPrice(Entity):
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return the unit of measurement of this entity.""" """Return the unit of measurement of this entity."""
return self._unit_of_measurement return self._tibber_home.price_unit
@property @property
def unique_id(self): def unique_id(self):
@ -139,17 +144,8 @@ class TibberSensorElPrice(Entity):
]["estimatedAnnualConsumption"] ]["estimatedAnnualConsumption"]
class TibberSensorRT(Entity): class TibberSensorRT(TibberSensor):
"""Representation of an Tibber sensor for real time consumption.""" """Representation of a 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}"
async def async_added_to_hass(self): async def async_added_to_hass(self):
"""Start unavailability tracking.""" """Start unavailability tracking."""
@ -175,11 +171,6 @@ class TibberSensorRT(Entity):
self.async_schedule_update_ha_state() self.async_schedule_update_ha_state()
@property
def device_state_attributes(self):
"""Return the state attributes."""
return self._device_state_attributes
@property @property
def available(self): def available(self):
"""Return True if entity is available.""" """Return True if entity is available."""
@ -188,18 +179,13 @@ class TibberSensorRT(Entity):
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""
return self._name return "Real time consumption {}".format(self._name)
@property @property
def should_poll(self): def should_poll(self):
"""Return the polling state.""" """Return the polling state."""
return False return False
@property
def state(self):
"""Return the state of the device."""
return self._state
@property @property
def icon(self): def icon(self):
"""Return the icon to use in the frontend.""" """Return the icon to use in the frontend."""
@ -208,7 +194,7 @@ class TibberSensorRT(Entity):
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return the unit of measurement of this entity.""" """Return the unit of measurement of this entity."""
return self._unit_of_measurement return "W"
@property @property
def unique_id(self): def unique_id(self):