Add support for Z-Wave battery level (#26943)

* Add support for Z-Wave battery level

* Improve coverage
This commit is contained in:
Andrew Onyshchuk 2019-09-27 12:21:04 -05:00 committed by Paulus Schoutsen
parent b1a9fa47ca
commit eeffd090a3
3 changed files with 30 additions and 1 deletions

View file

@ -277,6 +277,7 @@ DISCOVERY_SCHEMAS = [
const.COMMAND_CLASS_ALARM,
const.COMMAND_CLASS_SENSOR_ALARM,
const.COMMAND_CLASS_INDICATOR,
const.COMMAND_CLASS_BATTERY,
],
const.DISC_GENRE: const.GENRE_USER,
}

View file

@ -1,7 +1,7 @@
"""Support for Z-Wave sensors."""
import logging
from homeassistant.core import callback
from homeassistant.components.sensor import DOMAIN
from homeassistant.components.sensor import DOMAIN, DEVICE_CLASS_BATTERY
from homeassistant.const import TEMP_CELSIUS, TEMP_FAHRENHEIT
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from . import const, ZWaveDeviceEntity
@ -28,6 +28,8 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
def get_device(node, values, **kwargs):
"""Create Z-Wave entity device."""
# Generic Device mappings
if values.primary.command_class == const.COMMAND_CLASS_BATTERY:
return ZWaveBatterySensor(values)
if node.has_command_class(const.COMMAND_CLASS_SENSOR_MULTILEVEL):
return ZWaveMultilevelSensor(values)
if (
@ -107,3 +109,12 @@ class ZWaveAlarmSensor(ZWaveSensor):
"""
pass
class ZWaveBatterySensor(ZWaveSensor):
"""Representation of Z-Wave device battery level."""
@property
def device_class(self):
"""Return the class of this device."""
return DEVICE_CLASS_BATTERY

View file

@ -53,6 +53,23 @@ def test_get_device_detects_multilevel_meter(mock_openzwave):
assert isinstance(device, sensor.ZWaveMultilevelSensor)
def test_get_device_detects_battery_sensor(mock_openzwave):
"""Test get_device returns a Z-Wave battery sensor."""
node = MockNode(command_classes=[const.COMMAND_CLASS_BATTERY])
value = MockValue(
data=0,
node=node,
type=const.TYPE_DECIMAL,
command_class=const.COMMAND_CLASS_BATTERY,
)
values = MockEntityValues(primary=value)
device = sensor.get_device(node=node, values=values, node_config={})
assert isinstance(device, sensor.ZWaveBatterySensor)
assert device.device_class == homeassistant.const.DEVICE_CLASS_BATTERY
def test_multilevelsensor_value_changed_temp_fahrenheit(mock_openzwave):
"""Test value changed for Z-Wave multilevel sensor for temperature."""
node = MockNode(