Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 27 additions & 41 deletions homeassistant/components/tibber/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -54,10 +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
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."""
Expand Down Expand Up @@ -86,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."""
Expand All @@ -99,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):
Expand All @@ -114,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):
Expand All @@ -139,17 +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"
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."""
Expand All @@ -175,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."""
Expand All @@ -188,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."""
Expand All @@ -208,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):
Expand Down