Use entity class attributes for Blink (#52891)

* Use entity class attributes for blink

* rework

* revert extra state attributes
This commit is contained in:
Robert Hillis 2021-07-18 17:21:12 -04:00 committed by GitHub
parent 0cf95bb0c2
commit 73976d2a26
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 118 deletions

View file

@ -29,56 +29,28 @@ async def async_setup_entry(hass, config, async_add_entities):
class BlinkSyncModule(AlarmControlPanelEntity):
"""Representation of a Blink Alarm Control Panel."""
_attr_icon = ICON
_attr_supported_features = SUPPORT_ALARM_ARM_AWAY
def __init__(self, data, name, sync):
"""Initialize the alarm control panel."""
self.data = data
self.sync = sync
self._name = name
self._state = None
@property
def unique_id(self):
"""Return the unique id for the sync module."""
return self.sync.serial
@property
def icon(self):
"""Return icon."""
return ICON
@property
def state(self):
"""Return the state of the device."""
return self._state
@property
def supported_features(self) -> int:
"""Return the list of supported features."""
return SUPPORT_ALARM_ARM_AWAY
@property
def name(self):
"""Return the name of the panel."""
return f"{DOMAIN} {self._name}"
@property
def extra_state_attributes(self):
"""Return the state attributes."""
attr = self.sync.attributes
attr["network_info"] = self.data.networks
attr["associated_cameras"] = list(self.sync.cameras)
attr[ATTR_ATTRIBUTION] = DEFAULT_ATTRIBUTION
return attr
self._attr_unique_id = sync.serial
self._attr_name = f"{DOMAIN} {name}"
def update(self):
"""Update the state of the device."""
_LOGGER.debug("Updating Blink Alarm Control Panel %s", self._name)
self.data.refresh()
mode = self.sync.arm
if mode:
self._state = STATE_ALARM_ARMED_AWAY
else:
self._state = STATE_ALARM_DISARMED
self._attr_state = (
STATE_ALARM_ARMED_AWAY if self.sync.arm else STATE_ALARM_DISARMED
)
self.sync.attributes["network_info"] = self.data.networks
self.sync.attributes["associated_cameras"] = list(self.sync.cameras)
self.sync.attributes[ATTR_ATTRIBUTION] = DEFAULT_ATTRIBUTION
self._attr_extra_state_attributes = self.sync.attributes
def alarm_disarm(self, code=None):
"""Send disarm command."""

View file

@ -33,31 +33,10 @@ class BlinkBinarySensor(BinarySensorEntity):
self.data = data
self._type = sensor_type
name, device_class = BINARY_SENSORS[sensor_type]
self._name = f"{DOMAIN} {camera} {name}"
self._device_class = device_class
self._attr_name = f"{DOMAIN} {camera} {name}"
self._attr_device_class = device_class
self._camera = data.cameras[camera]
self._state = None
self._unique_id = f"{self._camera.serial}-{self._type}"
@property
def name(self):
"""Return the name of the blink sensor."""
return self._name
@property
def unique_id(self):
"""Return the unique id of the sensor."""
return self._unique_id
@property
def device_class(self):
"""Return the class of this device."""
return self._device_class
@property
def is_on(self):
"""Return the status of the sensor."""
return self._state
self._attr_unique_id = f"{self._camera.serial}-{sensor_type}"
def update(self):
"""Update sensor state."""
@ -65,4 +44,4 @@ class BlinkBinarySensor(BinarySensorEntity):
state = self._camera.attributes[self._type]
if self._type == TYPE_BATTERY:
state = state != "ok"
self._state = state
self._attr_is_on = state

View file

@ -32,23 +32,10 @@ class BlinkCamera(Camera):
"""Initialize a camera."""
super().__init__()
self.data = data
self._name = f"{DOMAIN} {name}"
self._attr_name = f"{DOMAIN} {name}"
self._camera = camera
self._unique_id = f"{camera.serial}-camera"
self.response = None
self.current_image = None
self.last_image = None
_LOGGER.debug("Initialized blink camera %s", self._name)
@property
def name(self):
"""Return the camera name."""
return self._name
@property
def unique_id(self):
"""Return the unique camera id."""
return self._unique_id
self._attr_unique_id = f"{camera.serial}-camera"
_LOGGER.debug("Initialized blink camera %s", self.name)
@property
def extra_state_attributes(self):

View file

@ -40,51 +40,23 @@ class BlinkSensor(SensorEntity):
def __init__(self, data, camera, sensor_type):
"""Initialize sensors from Blink camera."""
name, units, device_class = SENSORS[sensor_type]
self._name = f"{DOMAIN} {camera} {name}"
self._camera_name = name
self._type = sensor_type
self._device_class = device_class
self._attr_name = f"{DOMAIN} {camera} {name}"
self._attr_device_class = device_class
self.data = data
self._camera = data.cameras[camera]
self._state = None
self._unit_of_measurement = units
self._unique_id = f"{self._camera.serial}-{self._type}"
self._sensor_key = self._type
if self._type == "temperature":
self._sensor_key = "temperature_calibrated"
@property
def name(self):
"""Return the name of the camera."""
return self._name
@property
def unique_id(self):
"""Return the unique id for the camera sensor."""
return self._unique_id
@property
def state(self):
"""Return the camera's current state."""
return self._state
@property
def device_class(self):
"""Return the device's class."""
return self._device_class
@property
def unit_of_measurement(self):
"""Return the unit of measurement."""
return self._unit_of_measurement
self._attr_unit_of_measurement = units
self._attr_unique_id = f"{self._camera.serial}-{sensor_type}"
self._sensor_key = (
"temperature_calibrated" if sensor_type == "temperature" else sensor_type
)
def update(self):
"""Retrieve sensor data from the camera."""
self.data.refresh()
try:
self._state = self._camera.attributes[self._sensor_key]
self._attr_state = self._camera.attributes[self._sensor_key]
except KeyError:
self._state = None
self._attr_state = None
_LOGGER.error(
"%s not a valid camera attribute. Did the API change?", self._sensor_key
)