From 1bdc32b49935bf58400df544cd6bea8f4789b25d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=B8yer=20Iversen?= Date: Tue, 17 Sep 2019 07:41:37 +0200 Subject: [PATCH 1/2] Add alternative name for Tibber sensors --- homeassistant/components/tibber/sensor.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/tibber/sensor.py b/homeassistant/components/tibber/sensor.py index 3dfe0265bdeef..009dad9a8ca1a 100644 --- a/homeassistant/components/tibber/sensor.py +++ b/homeassistant/components/tibber/sensor.py @@ -55,9 +55,12 @@ def __init__(self, tibber_home): 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"] - ) + if tibber_home.info["viewer"]["home"]["appNickname"] is None: + name = tibber_home.info["viewer"]["home"]["address"].get("address1", "") + else: + name = tibber_home.info["viewer"]["home"]["appNickname"] + + self._name = "Electricity price {}".format(name) async def async_update(self): """Get the latest data and updates the states.""" @@ -148,8 +151,12 @@ def __init__(self, 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}" + if tibber_home.info["viewer"]["home"]["appNickname"] is None: + name = tibber_home.info["viewer"]["home"]["address"].get("address1", "") + else: + name = tibber_home.info["viewer"]["home"]["appNickname"] + + self._name = "Real time consumption {}".format(name) async def async_added_to_hass(self): """Start unavailability tracking.""" From 9c2a3dca2d3f9ce635182e2ec5e578a345e8d687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=B8yer=20Iversen?= Date: Tue, 17 Sep 2019 20:36:38 +0200 Subject: [PATCH 2/2] refactor tibber sensor --- homeassistant/components/tibber/sensor.py | 73 ++++++++--------------- 1 file changed, 26 insertions(+), 47 deletions(-) diff --git a/homeassistant/components/tibber/sensor.py b/homeassistant/components/tibber/sensor.py index 009dad9a8ca1a..a5a7f320d9331 100644 --- a/homeassistant/components/tibber/sensor.py +++ b/homeassistant/components/tibber/sensor.py @@ -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,13 +54,25 @@ def __init__(self, tibber_home): self._state = None self._is_available = False self._device_state_attributes = {} - self._unit_of_measurement = self._tibber_home.price_unit - if tibber_home.info["viewer"]["home"]["appNickname"] is None: - name = tibber_home.info["viewer"]["home"]["address"].get("address1", "") - else: - name = 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 - self._name = "Electricity price {}".format(name) + +class TibberSensorElPrice(TibberSensor): + """Representation of a Tibber sensor for el price.""" async def async_update(self): """Get the latest data and updates the states.""" @@ -89,11 +101,6 @@ async def async_update(self): 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.""" @@ -102,12 +109,7 @@ def available(self): @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): @@ -117,7 +119,7 @@ def icon(self): @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): @@ -142,21 +144,8 @@ async def _fetch_data(self): ]["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" - if tibber_home.info["viewer"]["home"]["appNickname"] is None: - name = tibber_home.info["viewer"]["home"]["address"].get("address1", "") - else: - name = tibber_home.info["viewer"]["home"]["appNickname"] - - self._name = "Real time consumption {}".format(name) +class TibberSensorRT(TibberSensor): + """Representation of a Tibber sensor for real time consumption.""" async def async_added_to_hass(self): """Start unavailability tracking.""" @@ -182,11 +171,6 @@ async def _async_callback(self, payload): 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.""" @@ -195,18 +179,13 @@ def available(self): @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.""" @@ -215,7 +194,7 @@ def icon(self): @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):