Skip to content

Commit

Permalink
Clean up SimpliSafe device info and sensor creation (home-assistant#4…
Browse files Browse the repository at this point in the history
…1920)

* Clean up SimpliSafe device info and sensor creation

* Code review
  • Loading branch information
bachya authored Oct 16, 2020
1 parent 1c3ec69 commit bbef87d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 44 deletions.
17 changes: 9 additions & 8 deletions homeassistant/components/simplisafe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,14 @@ def __init__(self, simplisafe, system, name, *, serial=None):
ATTR_SYSTEM_ID: system.system_id,
}

self._device_info = {
"identifiers": {(DOMAIN, system.system_id)},
"manufacturer": "SimpliSafe",
"model": system.version,
"name": name,
"via_device": (DOMAIN, system.serial),
}

@property
def available(self):
"""Return whether the entity is available."""
Expand All @@ -620,13 +628,7 @@ def available(self):
@property
def device_info(self):
"""Return device registry information for this entity."""
return {
"identifiers": {(DOMAIN, self._system.system_id)},
"manufacturer": "SimpliSafe",
"model": self._system.version,
"name": self._name,
"via_device": (DOMAIN, self._system.serial),
}
return self._device_info

@property
def device_state_attributes(self):
Expand Down Expand Up @@ -730,4 +732,3 @@ def async_update_from_rest_api(self):
@callback
def async_update_from_websocket_event(self, event):
"""Update the entity with the provided websocket event."""
raise NotImplementedError()
38 changes: 12 additions & 26 deletions homeassistant/components/simplisafe/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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."""
Expand All @@ -122,15 +117,6 @@ def unique_id(self):
"""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."""
Expand Down
14 changes: 4 additions & 10 deletions homeassistant/components/simplisafe/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ class SimplisafeFreezeSensor(SimpliSafeEntity):
def __init__(self, simplisafe, system, sensor):
"""Initialize."""
super().__init__(simplisafe, system, sensor.name, serial=sensor.serial)
self._system = system
self._sensor = 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."""
Expand All @@ -42,15 +45,6 @@ def unique_id(self):
"""Return unique ID of sensor."""
return self._sensor.serial

@property
def device_info(self):
"""Return device registry information for this entity."""
info = super().device_info
info["identifiers"] = {(DOMAIN, self._sensor.serial)}
info["model"] = "Freeze Sensor"
info["name"] = self._sensor.name
return info

@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
Expand Down

0 comments on commit bbef87d

Please sign in to comment.