From 51e032a7ca8d7af87215b60850f5b90ba381b51a Mon Sep 17 00:00:00 2001 From: brefra Date: Sun, 26 Jan 2020 18:48:20 +0100 Subject: [PATCH] Add counters sensors for Velbus (#31165) * Add counter sensor * Apply black formatting * Move all counter logic to sensor.py * Code cleanup --- homeassistant/components/velbus/__init__.py | 4 --- homeassistant/components/velbus/sensor.py | 32 +++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/velbus/__init__.py b/homeassistant/components/velbus/__init__.py index 8e00bc3fee5..d8f9dae13de 100644 --- a/homeassistant/components/velbus/__init__.py +++ b/homeassistant/components/velbus/__init__.py @@ -30,13 +30,11 @@ async def async_setup(hass, config): # Import from the configuration file if needed if DOMAIN not in config: return True - port = config[DOMAIN].get(CONF_PORT) data = {} if port: data = {CONF_PORT: port, CONF_NAME: "Velbus import"} - hass.async_create_task( hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_IMPORT}, data=data @@ -55,7 +53,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): discovery_info = {"cntrl": controller} for category in COMPONENT_TYPES: discovery_info[category] = [] - for module in modules: for channel in range(1, module.number_of_channels() + 1): for category in COMPONENT_TYPES: @@ -63,7 +60,6 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): discovery_info[category].append( (module.get_module_address(), channel) ) - hass.data[DOMAIN][entry.entry_id] = discovery_info for category in COMPONENT_TYPES: diff --git a/homeassistant/components/velbus/sensor.py b/homeassistant/components/velbus/sensor.py index 7ebdda2d781..d8644b4569a 100644 --- a/homeassistant/components/velbus/sensor.py +++ b/homeassistant/components/velbus/sensor.py @@ -1,6 +1,8 @@ """Support for Velbus sensors.""" import logging +from homeassistant.const import DEVICE_CLASS_POWER, ENERGY_KILO_WATT_HOUR + from . import VelbusEntity from .const import DOMAIN @@ -15,23 +17,53 @@ async def async_setup_entry(hass, entry, async_add_entities): for address, channel in modules_data: module = cntrl.get_module(address) entities.append(VelbusSensor(module, channel)) + if module.get_class(channel) == "counter": + entities.append(VelbusSensor(module, channel, True)) async_add_entities(entities) class VelbusSensor(VelbusEntity): """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 def device_class(self): """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) @property def state(self): """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) @property def unit_of_measurement(self): """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) + + @property + def icon(self): + """Icon to use in the frontend.""" + if self._is_counter: + return "mdi:counter" + return None