Fix missing sensor exceptions in SimpliSafe (#42845)

* Fix missing sensor exceptions in SimpliSafe

* Entity types

* Don't overdo it
This commit is contained in:
Aaron Bach 2020-11-04 12:34:14 -07:00 committed by GitHub
parent b0a6ac7e91
commit 5b754c29e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 44 deletions

View File

@ -728,3 +728,15 @@ class SimpliSafeEntity(CoordinatorEntity):
@callback
def async_update_from_websocket_event(self, event):
"""Update the entity with the provided websocket event."""
class SimpliSafeBaseSensor(SimpliSafeEntity):
"""Define a SimpliSafe base (binary) sensor."""
def __init__(self, simplisafe, system, sensor):
"""Initialize."""
super().__init__(simplisafe, system, sensor.name, serial=sensor.serial)
self._device_info["identifiers"] = {(DOMAIN, sensor.serial)}
self._device_info["model"] = sensor.type.name
self._device_info["name"] = sensor.name
self._sensor = sensor

View File

@ -11,7 +11,7 @@ from homeassistant.components.binary_sensor import (
)
from homeassistant.core import callback
from . import SimpliSafeEntity
from . import SimpliSafeBaseSensor
from .const import DATA_CLIENT, DOMAIN, LOGGER
SUPPORTED_BATTERY_SENSOR_TYPES = [
@ -23,27 +23,13 @@ SUPPORTED_BATTERY_SENSOR_TYPES = [
EntityTypes.temperature,
]
SUPPORTED_TRIGGERED_SENSOR_TYPES = [
EntityTypes.carbon_monoxide,
EntityTypes.entry,
EntityTypes.leak,
EntityTypes.smoke,
]
DEVICE_CLASSES = {
TRIGGERED_SENSOR_TYPES = {
EntityTypes.carbon_monoxide: DEVICE_CLASS_GAS,
EntityTypes.entry: DEVICE_CLASS_DOOR,
EntityTypes.leak: DEVICE_CLASS_MOISTURE,
EntityTypes.smoke: DEVICE_CLASS_SMOKE,
}
SENSOR_MODELS = {
EntityTypes.carbon_monoxide: "Carbon Monoxide Detector",
EntityTypes.entry: "Entry Sensor",
EntityTypes.leak: "Water Sensor",
EntityTypes.smoke: "Smoke Detector",
}
async def async_setup_entry(hass, entry, async_add_entities):
"""Set up SimpliSafe binary sensors based on a config entry."""
@ -56,39 +42,34 @@ async def async_setup_entry(hass, entry, async_add_entities):
continue
for sensor in system.sensors.values():
if sensor.type in SUPPORTED_TRIGGERED_SENSOR_TYPES:
sensors.append(TriggeredBinarySensor(simplisafe, system, sensor))
if sensor.type in TRIGGERED_SENSOR_TYPES:
sensors.append(
TriggeredBinarySensor(
simplisafe,
system,
sensor,
TRIGGERED_SENSOR_TYPES[sensor.type],
)
)
if sensor.type in SUPPORTED_BATTERY_SENSOR_TYPES:
sensors.append(BatteryBinarySensor(simplisafe, system, sensor))
async_add_entities(sensors)
class SimpliSafeBinarySensor(SimpliSafeEntity, BinarySensorEntity):
"""Define a SimpliSafe binary sensor entity."""
def __init__(self, simplisafe, system, sensor):
"""Initialize."""
super().__init__(simplisafe, system, sensor.name, serial=sensor.serial)
self._device_info["identifiers"] = {(DOMAIN, sensor.serial)}
self._device_info["model"] = SENSOR_MODELS[sensor.type]
self._device_info["name"] = sensor.name
class TriggeredBinarySensor(SimpliSafeBinarySensor):
class TriggeredBinarySensor(SimpliSafeBaseSensor, BinarySensorEntity):
"""Define a binary sensor related to whether an entity has been triggered."""
def __init__(self, simplisafe, system, sensor):
def __init__(self, simplisafe, system, sensor, device_class):
"""Initialize."""
super().__init__(simplisafe, system, sensor)
self._system = system
self._sensor = sensor
self._device_class = device_class
self._is_on = False
@property
def device_class(self):
"""Return type of sensor."""
return DEVICE_CLASSES[self._sensor.type]
return self._device_class
@property
def is_on(self):
@ -101,13 +82,12 @@ class TriggeredBinarySensor(SimpliSafeBinarySensor):
self._is_on = self._sensor.triggered
class BatteryBinarySensor(SimpliSafeEntity, BinarySensorEntity):
class BatteryBinarySensor(SimpliSafeBaseSensor, BinarySensorEntity):
"""Define a SimpliSafe battery binary sensor entity."""
def __init__(self, simplisafe, system, sensor):
"""Initialize."""
super().__init__(simplisafe, system, sensor)
self._sensor = sensor
self._is_low = False
@property

View File

@ -4,7 +4,7 @@ from simplipy.entity import EntityTypes
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_FAHRENHEIT
from homeassistant.core import callback
from . import SimpliSafeEntity
from . import SimpliSafeBaseSensor
from .const import DATA_CLIENT, DOMAIN, LOGGER
@ -25,19 +25,14 @@ async def async_setup_entry(hass, entry, async_add_entities):
async_add_entities(sensors)
class SimplisafeFreezeSensor(SimpliSafeEntity):
class SimplisafeFreezeSensor(SimpliSafeBaseSensor):
"""Define a SimpliSafe freeze sensor entity."""
def __init__(self, simplisafe, system, sensor):
"""Initialize."""
super().__init__(simplisafe, system, sensor.name, serial=sensor.serial)
self._sensor = sensor
super().__init__(simplisafe, system, sensor)
self._state = None
self._device_info["identifiers"] = {(DOMAIN, sensor.serial)}
self._device_info["model"] = "Freeze Sensor"
self._device_info["name"] = sensor.name
@property
def device_class(self):
"""Return type of sensor."""