Use _attr_* in android_ip_webcam ()

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-12-08 22:34:05 +01:00 committed by GitHub
parent ebf9faac17
commit 3e78c28a5b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 58 deletions
homeassistant/components/android_ip_webcam

View file

@ -1,6 +1,6 @@
"""Support for Android IP Webcam binary sensors."""
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_MOTION,
BinarySensorDeviceClass,
BinarySensorEntity,
)
@ -22,32 +22,18 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class IPWebcamBinarySensor(AndroidIPCamEntity, BinarySensorEntity):
"""Representation of an IP Webcam binary sensor."""
_attr_device_class = BinarySensorDeviceClass.MOTION
def __init__(self, name, host, ipcam, sensor):
"""Initialize the binary sensor."""
super().__init__(host, ipcam)
self._sensor = sensor
self._mapped_name = KEY_MAP.get(self._sensor, self._sensor)
self._name = f"{name} {self._mapped_name}"
self._state = None
self._unit = None
@property
def name(self):
"""Return the name of the binary sensor, if any."""
return self._name
@property
def is_on(self):
"""Return true if the binary sensor is on."""
return self._state
self._attr_name = f"{name} {self._mapped_name}"
self._attr_is_on = None
async def async_update(self):
"""Retrieve latest state."""
state, _ = self._ipcam.export_sensor(self._sensor)
self._state = state == 1.0
@property
def device_class(self):
"""Return the class of this device, from component DEVICE_CLASSES."""
return DEVICE_CLASS_MOTION
self._attr_is_on = state == 1.0

View file

@ -40,38 +40,26 @@ class IPWebcamSensor(AndroidIPCamEntity, SensorEntity):
self._sensor = sensor
self._mapped_name = KEY_MAP.get(self._sensor, self._sensor)
self._name = f"{name} {self._mapped_name}"
self._state = None
self._unit = None
@property
def name(self):
"""Return the name of the sensor, if any."""
return self._name
@property
def native_unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return self._unit
@property
def native_value(self):
"""Return the state of the sensor."""
return self._state
self._attr_name = f"{name} {self._mapped_name}"
self._attr_native_value = None
self._attr_native_unit_of_measurement = None
async def async_update(self):
"""Retrieve latest state."""
if self._sensor in ("audio_connections", "video_connections"):
if not self._ipcam.status_data:
return
self._state = self._ipcam.status_data.get(self._sensor)
self._unit = "Connections"
self._attr_native_value = self._ipcam.status_data.get(self._sensor)
self._attr_native_unit_of_measurement = "Connections"
else:
self._state, self._unit = self._ipcam.export_sensor(self._sensor)
(
self._attr_native_value,
self._attr_native_unit_of_measurement,
) = self._ipcam.export_sensor(self._sensor)
@property
def icon(self):
"""Return the icon for the sensor."""
if self._sensor == "battery_level" and self._state is not None:
return icon_for_battery_level(int(self._state))
if self._sensor == "battery_level" and self._attr_native_value is not None:
return icon_for_battery_level(int(self._attr_native_value))
return ICON_MAP.get(self._sensor, "mdi:eye")

View file

@ -39,22 +39,12 @@ class IPWebcamSettingsSwitch(AndroidIPCamEntity, SwitchEntity):
self._setting = setting
self._mapped_name = KEY_MAP.get(self._setting, self._setting)
self._name = f"{name} {self._mapped_name}"
self._state = False
@property
def name(self):
"""Return the name of the node."""
return self._name
self._attr_name = f"{name} {self._mapped_name}"
self._attr_is_on = False
async def async_update(self):
"""Get the updated status of the switch."""
self._state = bool(self._ipcam.current_settings.get(self._setting))
@property
def is_on(self):
"""Return the boolean response if the node is on."""
return self._state
self._attr_is_on = bool(self._ipcam.current_settings.get(self._setting))
async def async_turn_on(self, **kwargs):
"""Turn device on."""
@ -66,7 +56,7 @@ class IPWebcamSettingsSwitch(AndroidIPCamEntity, SwitchEntity):
await self._ipcam.record(record=True)
else:
await self._ipcam.change_setting(self._setting, True)
self._state = True
self._attr_is_on = True
self.async_write_ha_state()
async def async_turn_off(self, **kwargs):
@ -79,7 +69,7 @@ class IPWebcamSettingsSwitch(AndroidIPCamEntity, SwitchEntity):
await self._ipcam.record(record=False)
else:
await self._ipcam.change_setting(self._setting, False)
self._state = False
self._attr_is_on = False
self.async_write_ha_state()
@property