Add switch platform for Danfoss Air and additional sensors. (#21046)

* Add switch platform for Danfoss Air and additional sensors.

* Solve lint issues.

* Correct style.

* Minor changes

* Minor changes

* Minor changes

* Update file header

* Remove space

* Remove space
This commit is contained in:
Jonas Pedersen 2019-02-15 14:54:25 +01:00 committed by Fabian Affolter
parent 656d39e3ec
commit 7d0f847f83
5 changed files with 133 additions and 27 deletions

View File

@ -9,11 +9,11 @@ from homeassistant.helpers import discovery
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.util import Throttle from homeassistant.util import Throttle
REQUIREMENTS = ['pydanfossair==0.0.6'] REQUIREMENTS = ['pydanfossair==0.0.7']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DANFOSS_AIR_PLATFORMS = ['sensor', 'binary_sensor'] DANFOSS_AIR_PLATFORMS = ['sensor', 'binary_sensor', 'switch']
DOMAIN = 'danfoss_air' DOMAIN = 'danfoss_air'
MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60) MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
@ -52,6 +52,10 @@ class DanfossAir:
"""Get value for sensor.""" """Get value for sensor."""
return self._data.get(item) return self._data.get(item)
def update_state(self, command, state_command):
"""Send update command to Danfoss Air CCM."""
self._data[state_command] = self._client.command(command)
@Throttle(MIN_TIME_BETWEEN_UPDATES) @Throttle(MIN_TIME_BETWEEN_UPDATES)
def update(self): def update(self):
"""Use the data from Danfoss Air API.""" """Use the data from Danfoss Air API."""
@ -71,5 +75,17 @@ class DanfossAir:
= round(self._client.command(ReadCommand.filterPercent), 2) = round(self._client.command(ReadCommand.filterPercent), 2)
self._data[ReadCommand.bypass] \ self._data[ReadCommand.bypass] \
= self._client.command(ReadCommand.bypass) = self._client.command(ReadCommand.bypass)
self._data[ReadCommand.fan_step] \
= self._client.command(ReadCommand.fan_step)
self._data[ReadCommand.supply_fan_speed] \
= self._client.command(ReadCommand.supply_fan_speed)
self._data[ReadCommand.exhaust_fan_speed] \
= self._client.command(ReadCommand.exhaust_fan_speed)
self._data[ReadCommand.away_mode] \
= self._client.command(ReadCommand.away_mode)
self._data[ReadCommand.boost] \
= self._client.command(ReadCommand.boost)
self._data[ReadCommand.battery_percent] \
= self._client.command(ReadCommand.battery_percent)
_LOGGER.debug("Done fetching data from Danfoss Air CCM module") _LOGGER.debug("Done fetching data from Danfoss Air CCM module")

View File

@ -1,9 +1,4 @@
""" """Support for the for Danfoss Air HRV binary sensors."""
Support for the for Danfoss Air HRV binary sensor platform.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/binary_sensor.danfoss_air/
"""
from homeassistant.components.binary_sensor import BinarySensorDevice from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.components.danfoss_air import DOMAIN \ from homeassistant.components.danfoss_air import DOMAIN \
as DANFOSS_AIR_DOMAIN as DANFOSS_AIR_DOMAIN
@ -14,12 +9,16 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
from pydanfossair.commands import ReadCommand from pydanfossair.commands import ReadCommand
data = hass.data[DANFOSS_AIR_DOMAIN] data = hass.data[DANFOSS_AIR_DOMAIN]
sensors = [["Danfoss Air Bypass Active", ReadCommand.bypass]] sensors = [
["Danfoss Air Bypass Active", ReadCommand.bypass, "opening"],
["Danfoss Air Away Mode Active", ReadCommand.away_mode, None],
]
dev = [] dev = []
for sensor in sensors: for sensor in sensors:
dev.append(DanfossAirBinarySensor(data, sensor[0], sensor[1])) dev.append(DanfossAirBinarySensor(
data, sensor[0], sensor[1], sensor[2]))
add_entities(dev, True) add_entities(dev, True)
@ -27,12 +26,13 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class DanfossAirBinarySensor(BinarySensorDevice): class DanfossAirBinarySensor(BinarySensorDevice):
"""Representation of a Danfoss Air binary sensor.""" """Representation of a Danfoss Air binary sensor."""
def __init__(self, data, name, sensor_type): def __init__(self, data, name, sensor_type, device_class):
"""Initialize the Danfoss Air binary sensor.""" """Initialize the Danfoss Air binary sensor."""
self._data = data self._data = data
self._name = name self._name = name
self._state = None self._state = None
self._type = sensor_type self._type = sensor_type
self._device_class = device_class
@property @property
def name(self): def name(self):
@ -47,7 +47,7 @@ class DanfossAirBinarySensor(BinarySensorDevice):
@property @property
def device_class(self): def device_class(self):
"""Type of device class.""" """Type of device class."""
return "opening" return self._device_class
def update(self): def update(self):
"""Fetch new state data for the sensor.""" """Fetch new state data for the sensor."""

View File

@ -1,14 +1,15 @@
""" """Support for the for Danfoss Air HRV sensors."""
Support for the for Danfoss Air HRV sensor platform. import logging
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/sensor.danfoss_air/
"""
from homeassistant.components.danfoss_air import DOMAIN \ from homeassistant.components.danfoss_air import DOMAIN \
as DANFOSS_AIR_DOMAIN as DANFOSS_AIR_DOMAIN
from homeassistant.const import TEMP_CELSIUS from homeassistant.const import (
TEMP_CELSIUS, DEVICE_CLASS_BATTERY,
DEVICE_CLASS_TEMPERATURE, DEVICE_CLASS_HUMIDITY)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the available Danfoss Air sensors etc.""" """Set up the available Danfoss Air sensors etc."""
@ -18,23 +19,32 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
sensors = [ sensors = [
["Danfoss Air Exhaust Temperature", TEMP_CELSIUS, ["Danfoss Air Exhaust Temperature", TEMP_CELSIUS,
ReadCommand.exhaustTemperature], ReadCommand.exhaustTemperature, DEVICE_CLASS_TEMPERATURE],
["Danfoss Air Outdoor Temperature", TEMP_CELSIUS, ["Danfoss Air Outdoor Temperature", TEMP_CELSIUS,
ReadCommand.outdoorTemperature], ReadCommand.outdoorTemperature, DEVICE_CLASS_TEMPERATURE],
["Danfoss Air Supply Temperature", TEMP_CELSIUS, ["Danfoss Air Supply Temperature", TEMP_CELSIUS,
ReadCommand.supplyTemperature], ReadCommand.supplyTemperature, DEVICE_CLASS_TEMPERATURE],
["Danfoss Air Extract Temperature", TEMP_CELSIUS, ["Danfoss Air Extract Temperature", TEMP_CELSIUS,
ReadCommand.extractTemperature], ReadCommand.extractTemperature, DEVICE_CLASS_TEMPERATURE],
["Danfoss Air Remaining Filter", '%', ["Danfoss Air Remaining Filter", '%',
ReadCommand.filterPercent], ReadCommand.filterPercent, None],
["Danfoss Air Humidity", '%', ["Danfoss Air Humidity", '%',
ReadCommand.humidity] ReadCommand.humidity, DEVICE_CLASS_HUMIDITY],
["Danfoss Air Fan Step", '%',
ReadCommand.fan_step, None],
["Dandoss Air Exhaust Fan Speed", 'RPM',
ReadCommand.exhaust_fan_speed, None],
["Dandoss Air Supply Fan Speed", 'RPM',
ReadCommand.supply_fan_speed, None],
["Dandoss Air Dial Battery", '%',
ReadCommand.battery_percent, DEVICE_CLASS_BATTERY]
] ]
dev = [] dev = []
for sensor in sensors: for sensor in sensors:
dev.append(DanfossAir(data, sensor[0], sensor[1], sensor[2])) dev.append(DanfossAir(
data, sensor[0], sensor[1], sensor[2], sensor[3]))
add_entities(dev, True) add_entities(dev, True)
@ -42,19 +52,25 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class DanfossAir(Entity): class DanfossAir(Entity):
"""Representation of a Sensor.""" """Representation of a Sensor."""
def __init__(self, data, name, sensor_unit, sensor_type): def __init__(self, data, name, sensor_unit, sensor_type, device_class):
"""Initialize the sensor.""" """Initialize the sensor."""
self._data = data self._data = data
self._name = name self._name = name
self._state = None self._state = None
self._type = sensor_type self._type = sensor_type
self._unit = sensor_unit self._unit = sensor_unit
self._device_class = device_class
@property @property
def name(self): def name(self):
"""Return the name of the sensor.""" """Return the name of the sensor."""
return self._name return self._name
@property
def device_class(self):
"""Return the device class of the sensor."""
return self._device_class
@property @property
def state(self): def state(self):
"""Return the state of the sensor.""" """Return the state of the sensor."""
@ -74,3 +90,5 @@ class DanfossAir(Entity):
self._data.update() self._data.update()
self._state = self._data.get_value(self._type) self._state = self._data.get_value(self._type)
if self._state is None:
_LOGGER.debug("Could not get data for %s", self._type)

View File

@ -0,0 +1,72 @@
"""Support for the for Danfoss Air HRV sswitches."""
import logging
from homeassistant.components.switch import (
SwitchDevice)
from homeassistant.components.danfoss_air import DOMAIN \
as DANFOSS_AIR_DOMAIN
_LOGGER = logging.getLogger(__name__)
def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the Danfoss Air HRV switch platform."""
from pydanfossair.commands import ReadCommand, UpdateCommand
data = hass.data[DANFOSS_AIR_DOMAIN]
switches = [
["Danfoss Air Boost",
ReadCommand.boost,
UpdateCommand.boost_activate,
UpdateCommand.boost_deactivate],
]
dev = []
for switch in switches:
dev.append(DanfossAir(
data, switch[0], switch[1], switch[2], switch[3]))
add_entities(dev)
class DanfossAir(SwitchDevice):
"""Representation of a Danfoss Air HRV Switch."""
def __init__(self, data, name, state_command, on_command, off_command):
"""Initialize the switch."""
self._data = data
self._name = name
self._state_command = state_command
self._on_command = on_command
self._off_command = off_command
self._state = None
@property
def name(self):
"""Return the name of the switch."""
return self._name
@property
def is_on(self):
"""Return true if switch is on."""
return self._state
def turn_on(self, **kwargs):
"""Turn the switch on."""
_LOGGER.debug("Turning on switch with command %s", self._on_command)
self._data.update_state(self._on_command, self._state_command)
def turn_off(self, **kwargs):
"""Turn the switch off."""
_LOGGER.debug("Turning of switch with command %s", self._off_command)
self._data.update_state(self._off_command, self._state_command)
def update(self):
"""Update the switch's state."""
self._data.update()
self._state = self._data.get_value(self._state_command)
if self._state is None:
_LOGGER.debug("Could not get data for %s", self._state_command)

View File

@ -980,7 +980,7 @@ pycsspeechtts==1.0.2
pydaikin==0.9 pydaikin==0.9
# homeassistant.components.danfoss_air # homeassistant.components.danfoss_air
pydanfossair==0.0.6 pydanfossair==0.0.7
# homeassistant.components.deconz # homeassistant.components.deconz
pydeconz==47 pydeconz==47