Clean up SimpliSafe device info and sensor creation (#41920)

* Clean up SimpliSafe device info and sensor creation

* Code review
This commit is contained in:
Aaron Bach authored and GitHub committed 2020-10-16 12:01:16 -05:00
1 parent 1c3ec69166
commit bbef87d3f3
3 files changed
+25 -44

No files matched your search

@@ -48,23 +48,15 @@ async def async_setup_entry(hass, entry, async_add_entities):
"""Set up SimpliSafe binary sensors based on a config entry."""
simplisafe = hass.data[DOMAIN][DATA_CLIENT][entry.entry_id]
# Add sensor
sensors = [
SimpliSafeBinarySensor(simplisafe, system, sensor)
for system in simplisafe.systems.values()
for sensor in system.sensors.values()
if sensor.type in SUPPORTED_SENSOR_TYPES
]
sensors = []
for system in simplisafe.systems.values():
for sensor in system.sensors.values():
if sensor.type in SUPPORTED_SENSOR_TYPES:
sensors.append(SimpliSafeBinarySensor(simplisafe, system, sensor))
if sensor.type in SUPPORTED_BATTERY_SENSOR_TYPES:
sensors.append(SimpliSafeSensorBattery(simplisafe, system, sensor))
# Add low battery status entity for every sensor
battery_sensors = [
SimpliSafeSensorBattery(simplisafe, system, sensor)
for system in simplisafe.systems.values()
for sensor in system.sensors.values()
if sensor.type in SUPPORTED_BATTERY_SENSOR_TYPES
]
async_add_entities(sensors + battery_sensors)
async_add_entities(sensors)
class SimpliSafeBinarySensor(SimpliSafeEntity, BinarySensorEntity):
@@ -108,10 +100,13 @@ class SimpliSafeSensorBattery(SimpliSafeEntity, BinarySensorEntity):
def __init__(self, simplisafe, system, sensor):
"""Initialize."""
super().__init__(simplisafe, system, sensor.name, serial=sensor.serial)
self._system = system
self._sensor = sensor
self._is_low = False
self._device_info["identifiers"] = {(DOMAIN, sensor.serial)}
self._device_info["model"] = SENSOR_MODELS[sensor.type]
self._device_info["name"] = sensor.name
@property
def device_class(self):
"""Return type of sensor."""
@@ -122,15 +117,6 @@ class SimpliSafeSensorBattery(SimpliSafeEntity, BinarySensorEntity):
"""Return unique ID of sensor."""
return f"{self._sensor.serial}-battery"
@property
def device_info(self):
"""Return device registry information for this entity."""
info = super().device_info
info["identifiers"] = {(DOMAIN, self._sensor.serial)}
info["model"] = SENSOR_MODELS[self._sensor.type]
info["name"] = self._sensor.name
return info
@property
def is_on(self):
"""Return true if the battery is low."""