Add counters sensors for Velbus (#31165)

* Add counter sensor

* Apply black formatting

* Move all counter logic to sensor.py

* Code cleanup
This commit is contained in:
brefra 2020-01-26 18:48:20 +01:00 committed by Martin Hjelmare
parent 1b3c4ed4b3
commit 51e032a7ca
2 changed files with 32 additions and 4 deletions

View file

@ -30,13 +30,11 @@ async def async_setup(hass, config):
# Import from the configuration file if needed # Import from the configuration file if needed
if DOMAIN not in config: if DOMAIN not in config:
return True return True
port = config[DOMAIN].get(CONF_PORT) port = config[DOMAIN].get(CONF_PORT)
data = {} data = {}
if port: if port:
data = {CONF_PORT: port, CONF_NAME: "Velbus import"} data = {CONF_PORT: port, CONF_NAME: "Velbus import"}
hass.async_create_task( hass.async_create_task(
hass.config_entries.flow.async_init( hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=data DOMAIN, context={"source": SOURCE_IMPORT}, data=data
@ -55,7 +53,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
discovery_info = {"cntrl": controller} discovery_info = {"cntrl": controller}
for category in COMPONENT_TYPES: for category in COMPONENT_TYPES:
discovery_info[category] = [] discovery_info[category] = []
for module in modules: for module in modules:
for channel in range(1, module.number_of_channels() + 1): for channel in range(1, module.number_of_channels() + 1):
for category in COMPONENT_TYPES: for category in COMPONENT_TYPES:
@ -63,7 +60,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
discovery_info[category].append( discovery_info[category].append(
(module.get_module_address(), channel) (module.get_module_address(), channel)
) )
hass.data[DOMAIN][entry.entry_id] = discovery_info hass.data[DOMAIN][entry.entry_id] = discovery_info
for category in COMPONENT_TYPES: for category in COMPONENT_TYPES:

View file

@ -1,6 +1,8 @@
"""Support for Velbus sensors.""" """Support for Velbus sensors."""
import logging import logging
from homeassistant.const import DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR
from . import VelbusEntity from . import VelbusEntity
from .const import DOMAIN from .const import DOMAIN
@ -15,23 +17,53 @@ async def async_setup_entry(hass, entry, async_add_entities):
for address, channel in modules_data: for address, channel in modules_data:
module = cntrl.get_module(address) module = cntrl.get_module(address)
entities.append(VelbusSensor(module, channel)) entities.append(VelbusSensor(module, channel))
if module.get_class(channel) == "counter":
entities.append(VelbusSensor(module, channel, True))
async_add_entities(entities) async_add_entities(entities)
class VelbusSensor(VelbusEntity): class VelbusSensor(VelbusEntity):
"""Representation of a sensor.""" """Representation of a sensor."""
def __init__(self, module, channel, counter=False):
"""Initialize a sensor Velbus entity."""
super().__init__(module, channel)
self._is_counter = counter
@property
def unique_id(self):
"""Return unique ID for counter sensors."""
unique_id = super().unique_id
if self._is_counter:
unique_id = f"{unique_id}-counter"
return unique_id
@property @property
def device_class(self): def device_class(self):
"""Return the device class of the sensor.""" """Return the device class of the sensor."""
if self._module.get_class(self._channel) == "counter" and not self._is_counter:
if self._module.get_counter_unit(self._channel) == ENERGY_KILO_WATT_HOUR:
return DEVICE_CLASS_POWER
return None
return self._module.get_class(self._channel) return self._module.get_class(self._channel)
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
if self._is_counter:
return self._module.get_counter_state(self._channel)
return self._module.get_state(self._channel) return self._module.get_state(self._channel)
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return the unit this state is expressed in.""" """Return the unit this state is expressed in."""
if self._is_counter:
return self._module.get_counter_unit(self._channel)
return self._module.get_unit(self._channel) return self._module.get_unit(self._channel)
@property
def icon(self):
"""Icon to use in the frontend."""
if self._is_counter:
return "mdi:counter"
return None