Skip to content

Commit

Permalink
Use entity description for Ezviz sensors (home-assistant#56634)
Browse files Browse the repository at this point in the history
  • Loading branch information
RenierM26 authored Oct 11, 2021
1 parent b155d2b commit 48c2cfa
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 270 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ omit =
homeassistant/components/ezviz/camera.py
homeassistant/components/ezviz/coordinator.py
homeassistant/components/ezviz/const.py
homeassistant/components/ezviz/entity.py
homeassistant/components/ezviz/binary_sensor.py
homeassistant/components/ezviz/sensor.py
homeassistant/components/ezviz/switch.py
Expand Down
107 changes: 45 additions & 62 deletions homeassistant/components/ezviz/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
"""Support for Ezviz binary sensors."""
import logging

from pyezviz.constants import BinarySensorType

from homeassistant.components.binary_sensor import BinarySensorEntity
from __future__ import annotations

from homeassistant.components.binary_sensor import (
DEVICE_CLASS_MOTION,
DEVICE_CLASS_UPDATE,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from .const import DATA_COORDINATOR, DOMAIN, MANUFACTURER
from .const import DATA_COORDINATOR, DOMAIN
from .coordinator import EzvizDataUpdateCoordinator

_LOGGER = logging.getLogger(__name__)
from .entity import EzvizEntity

PARALLEL_UPDATES = 1

BINARY_SENSOR_TYPES: dict[str, BinarySensorEntityDescription] = {
"Motion_Trigger": BinarySensorEntityDescription(
key="Motion_Trigger",
device_class=DEVICE_CLASS_MOTION,
),
"alarm_schedules_enabled": BinarySensorEntityDescription(
key="alarm_schedules_enabled"
),
"encrypted": BinarySensorEntityDescription(key="encrypted"),
"upgrade_available": BinarySensorEntityDescription(
key="upgrade_available",
device_class=DEVICE_CLASS_UPDATE,
),
}


async def async_setup_entry(
Expand All @@ -23,71 +40,37 @@ async def async_setup_entry(
coordinator: EzvizDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id][
DATA_COORDINATOR
]
sensors = []

for idx, camera in enumerate(coordinator.data):
for name in camera:
# Only add sensor with value.
if camera.get(name) is None:
continue

if name in BinarySensorType.__members__:
sensor_type_name = getattr(BinarySensorType, name).value
sensors.append(
EzvizBinarySensor(coordinator, idx, name, sensor_type_name)
)
async_add_entities(
[
EzvizBinarySensor(coordinator, camera, binary_sensor)
for camera in coordinator.data
for binary_sensor, value in coordinator.data[camera].items()
if binary_sensor in BINARY_SENSOR_TYPES
if value is not None
]
)

async_add_entities(sensors)


class EzvizBinarySensor(CoordinatorEntity, BinarySensorEntity):
class EzvizBinarySensor(EzvizEntity, BinarySensorEntity):
"""Representation of a Ezviz sensor."""

coordinator: EzvizDataUpdateCoordinator

def __init__(
self,
coordinator: EzvizDataUpdateCoordinator,
idx: int,
name: str,
sensor_type_name: str,
serial: str,
binary_sensor: str,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator)
self._idx = idx
self._camera_name = self.coordinator.data[self._idx]["name"]
self._name = name
self._sensor_name = f"{self._camera_name}.{self._name}"
self.sensor_type_name = sensor_type_name
self._serial = self.coordinator.data[self._idx]["serial"]

@property
def name(self) -> str:
"""Return the name of the Ezviz sensor."""
return self._name
super().__init__(coordinator, serial)
self._sensor_name = binary_sensor
self._attr_name = f"{self._camera_name} {binary_sensor.title()}"
self._attr_unique_id = f"{serial}_{self._camera_name}.{binary_sensor}"
self.entity_description = BINARY_SENSOR_TYPES[binary_sensor]

@property
def is_on(self) -> bool:
"""Return the state of the sensor."""
return self.coordinator.data[self._idx][self._name]

@property
def unique_id(self) -> str:
"""Return the unique ID of this sensor."""
return f"{self._serial}_{self._sensor_name}"

@property
def device_info(self) -> DeviceInfo:
"""Return the device_info of the device."""
return {
"identifiers": {(DOMAIN, self._serial)},
"name": self.coordinator.data[self._idx]["name"],
"model": self.coordinator.data[self._idx]["device_sub_category"],
"manufacturer": MANUFACTURER,
"sw_version": self.coordinator.data[self._idx]["version"],
}

@property
def device_class(self) -> str:
"""Device class for the sensor."""
return self.sensor_type_name
return self.data[self._sensor_name]
Loading

0 comments on commit 48c2cfa

Please sign in to comment.