Use _attr_* in android_ip_webcam (#61270)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
ebf9faac17
commit
3e78c28a5b
3 changed files with 22 additions and 58 deletions
|
@ -1,6 +1,6 @@
|
||||||
"""Support for Android IP Webcam binary sensors."""
|
"""Support for Android IP Webcam binary sensors."""
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
DEVICE_CLASS_MOTION,
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -22,32 +22,18 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||||
class IPWebcamBinarySensor(AndroidIPCamEntity, BinarySensorEntity):
|
class IPWebcamBinarySensor(AndroidIPCamEntity, BinarySensorEntity):
|
||||||
"""Representation of an IP Webcam binary sensor."""
|
"""Representation of an IP Webcam binary sensor."""
|
||||||
|
|
||||||
|
_attr_device_class = BinarySensorDeviceClass.MOTION
|
||||||
|
|
||||||
def __init__(self, name, host, ipcam, sensor):
|
def __init__(self, name, host, ipcam, sensor):
|
||||||
"""Initialize the binary sensor."""
|
"""Initialize the binary sensor."""
|
||||||
super().__init__(host, ipcam)
|
super().__init__(host, ipcam)
|
||||||
|
|
||||||
self._sensor = sensor
|
self._sensor = sensor
|
||||||
self._mapped_name = KEY_MAP.get(self._sensor, self._sensor)
|
self._mapped_name = KEY_MAP.get(self._sensor, self._sensor)
|
||||||
self._name = f"{name} {self._mapped_name}"
|
self._attr_name = f"{name} {self._mapped_name}"
|
||||||
self._state = None
|
self._attr_is_on = 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
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Retrieve latest state."""
|
"""Retrieve latest state."""
|
||||||
state, _ = self._ipcam.export_sensor(self._sensor)
|
state, _ = self._ipcam.export_sensor(self._sensor)
|
||||||
self._state = state == 1.0
|
self._attr_is_on = state == 1.0
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self):
|
|
||||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
|
||||||
return DEVICE_CLASS_MOTION
|
|
||||||
|
|
|
@ -40,38 +40,26 @@ class IPWebcamSensor(AndroidIPCamEntity, SensorEntity):
|
||||||
|
|
||||||
self._sensor = sensor
|
self._sensor = sensor
|
||||||
self._mapped_name = KEY_MAP.get(self._sensor, self._sensor)
|
self._mapped_name = KEY_MAP.get(self._sensor, self._sensor)
|
||||||
self._name = f"{name} {self._mapped_name}"
|
self._attr_name = f"{name} {self._mapped_name}"
|
||||||
self._state = None
|
self._attr_native_value = None
|
||||||
self._unit = None
|
self._attr_native_unit_of_measurement = 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
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Retrieve latest state."""
|
"""Retrieve latest state."""
|
||||||
if self._sensor in ("audio_connections", "video_connections"):
|
if self._sensor in ("audio_connections", "video_connections"):
|
||||||
if not self._ipcam.status_data:
|
if not self._ipcam.status_data:
|
||||||
return
|
return
|
||||||
self._state = self._ipcam.status_data.get(self._sensor)
|
self._attr_native_value = self._ipcam.status_data.get(self._sensor)
|
||||||
self._unit = "Connections"
|
self._attr_native_unit_of_measurement = "Connections"
|
||||||
else:
|
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
|
@property
|
||||||
def icon(self):
|
def icon(self):
|
||||||
"""Return the icon for the sensor."""
|
"""Return the icon for the sensor."""
|
||||||
if self._sensor == "battery_level" and self._state is not None:
|
if self._sensor == "battery_level" and self._attr_native_value is not None:
|
||||||
return icon_for_battery_level(int(self._state))
|
return icon_for_battery_level(int(self._attr_native_value))
|
||||||
return ICON_MAP.get(self._sensor, "mdi:eye")
|
return ICON_MAP.get(self._sensor, "mdi:eye")
|
||||||
|
|
|
@ -39,22 +39,12 @@ class IPWebcamSettingsSwitch(AndroidIPCamEntity, SwitchEntity):
|
||||||
|
|
||||||
self._setting = setting
|
self._setting = setting
|
||||||
self._mapped_name = KEY_MAP.get(self._setting, self._setting)
|
self._mapped_name = KEY_MAP.get(self._setting, self._setting)
|
||||||
self._name = f"{name} {self._mapped_name}"
|
self._attr_name = f"{name} {self._mapped_name}"
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self):
|
|
||||||
"""Return the name of the node."""
|
|
||||||
return self._name
|
|
||||||
|
|
||||||
async def async_update(self):
|
async def async_update(self):
|
||||||
"""Get the updated status of the switch."""
|
"""Get the updated status of the switch."""
|
||||||
self._state = bool(self._ipcam.current_settings.get(self._setting))
|
self._attr_is_on = 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
|
|
||||||
|
|
||||||
async def async_turn_on(self, **kwargs):
|
async def async_turn_on(self, **kwargs):
|
||||||
"""Turn device on."""
|
"""Turn device on."""
|
||||||
|
@ -66,7 +56,7 @@ class IPWebcamSettingsSwitch(AndroidIPCamEntity, SwitchEntity):
|
||||||
await self._ipcam.record(record=True)
|
await self._ipcam.record(record=True)
|
||||||
else:
|
else:
|
||||||
await self._ipcam.change_setting(self._setting, True)
|
await self._ipcam.change_setting(self._setting, True)
|
||||||
self._state = True
|
self._attr_is_on = True
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
async def async_turn_off(self, **kwargs):
|
async def async_turn_off(self, **kwargs):
|
||||||
|
@ -79,7 +69,7 @@ class IPWebcamSettingsSwitch(AndroidIPCamEntity, SwitchEntity):
|
||||||
await self._ipcam.record(record=False)
|
await self._ipcam.record(record=False)
|
||||||
else:
|
else:
|
||||||
await self._ipcam.change_setting(self._setting, False)
|
await self._ipcam.change_setting(self._setting, False)
|
||||||
self._state = False
|
self._attr_is_on = False
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue