From 798f487ea4455d7d58ddafc518cbaeb071472ee8 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 3 Sep 2021 22:34:29 +0200 Subject: [PATCH] Use EntityDescription - faa_delays (#55678) * Use EntityDescription - faa_delays * Update binary_sensor.py --- .../components/faa_delays/binary_sensor.py | 75 ++++++++----------- homeassistant/components/faa_delays/const.py | 52 +++++++------ 2 files changed, 62 insertions(+), 65 deletions(-) diff --git a/homeassistant/components/faa_delays/binary_sensor.py b/homeassistant/components/faa_delays/binary_sensor.py index b96ee24a5bc..9ef55a1f2cc 100644 --- a/homeassistant/components/faa_delays/binary_sensor.py +++ b/homeassistant/components/faa_delays/binary_sensor.py @@ -1,6 +1,12 @@ """Platform for FAA Delays sensor component.""" -from homeassistant.components.binary_sensor import BinarySensorEntity -from homeassistant.const import ATTR_ICON, ATTR_NAME +from __future__ import annotations + +from typing import Any + +from homeassistant.components.binary_sensor import ( + BinarySensorEntity, + BinarySensorEntityDescription, +) from homeassistant.helpers.update_coordinator import CoordinatorEntity from .const import DOMAIN, FAA_BINARY_SENSORS @@ -10,83 +16,68 @@ async def async_setup_entry(hass, entry, async_add_entities): """Set up a FAA sensor based on a config entry.""" coordinator = hass.data[DOMAIN][entry.entry_id] - binary_sensors = [] - for kind, attrs in FAA_BINARY_SENSORS.items(): - name = attrs[ATTR_NAME] - icon = attrs[ATTR_ICON] + entities = [ + FAABinarySensor(coordinator, entry.entry_id, description) + for description in FAA_BINARY_SENSORS + ] - binary_sensors.append( - FAABinarySensor(coordinator, kind, name, icon, entry.entry_id) - ) - - async_add_entities(binary_sensors) + async_add_entities(entities) class FAABinarySensor(CoordinatorEntity, BinarySensorEntity): """Define a binary sensor for FAA Delays.""" - def __init__(self, coordinator, sensor_type, name, icon, entry_id): + def __init__( + self, coordinator, entry_id, description: BinarySensorEntityDescription + ): """Initialize the sensor.""" super().__init__(coordinator) + self.entity_description = description self.coordinator = coordinator self._entry_id = entry_id - self._icon = icon - self._name = name - self._sensor_type = sensor_type - self._id = self.coordinator.data.iata - self._attrs = {} - - @property - def name(self): - """Return the name of the sensor.""" - return f"{self._id} {self._name}" - - @property - def icon(self): - """Return the icon.""" - return self._icon + self._attrs: dict[str, Any] = {} + _id = coordinator.data.iata + self._attr_name = f"{_id} {description.name}" + self._attr_unique_id = f"{_id}_{description.key}" @property def is_on(self): """Return the status of the sensor.""" - if self._sensor_type == "GROUND_DELAY": + sensor_type = self.entity_description.key + if sensor_type == "GROUND_DELAY": return self.coordinator.data.ground_delay.status - if self._sensor_type == "GROUND_STOP": + if sensor_type == "GROUND_STOP": return self.coordinator.data.ground_stop.status - if self._sensor_type == "DEPART_DELAY": + if sensor_type == "DEPART_DELAY": return self.coordinator.data.depart_delay.status - if self._sensor_type == "ARRIVE_DELAY": + if sensor_type == "ARRIVE_DELAY": return self.coordinator.data.arrive_delay.status - if self._sensor_type == "CLOSURE": + if sensor_type == "CLOSURE": return self.coordinator.data.closure.status return None - @property - def unique_id(self): - """Return a unique, Home Assistant friendly identifier for this entity.""" - return f"{self._id}_{self._sensor_type}" - @property def extra_state_attributes(self): """Return attributes for sensor.""" - if self._sensor_type == "GROUND_DELAY": + sensor_type = self.entity_description.key + if sensor_type == "GROUND_DELAY": self._attrs["average"] = self.coordinator.data.ground_delay.average self._attrs["reason"] = self.coordinator.data.ground_delay.reason - elif self._sensor_type == "GROUND_STOP": + elif sensor_type == "GROUND_STOP": self._attrs["endtime"] = self.coordinator.data.ground_stop.endtime self._attrs["reason"] = self.coordinator.data.ground_stop.reason - elif self._sensor_type == "DEPART_DELAY": + elif sensor_type == "DEPART_DELAY": self._attrs["minimum"] = self.coordinator.data.depart_delay.minimum self._attrs["maximum"] = self.coordinator.data.depart_delay.maximum self._attrs["trend"] = self.coordinator.data.depart_delay.trend self._attrs["reason"] = self.coordinator.data.depart_delay.reason - elif self._sensor_type == "ARRIVE_DELAY": + elif sensor_type == "ARRIVE_DELAY": self._attrs["minimum"] = self.coordinator.data.arrive_delay.minimum self._attrs["maximum"] = self.coordinator.data.arrive_delay.maximum self._attrs["trend"] = self.coordinator.data.arrive_delay.trend self._attrs["reason"] = self.coordinator.data.arrive_delay.reason - elif self._sensor_type == "CLOSURE": + elif sensor_type == "CLOSURE": self._attrs["begin"] = self.coordinator.data.closure.begin self._attrs["end"] = self.coordinator.data.closure.end self._attrs["reason"] = self.coordinator.data.closure.reason diff --git a/homeassistant/components/faa_delays/const.py b/homeassistant/components/faa_delays/const.py index c725be88106..f7ee8e7bad8 100644 --- a/homeassistant/components/faa_delays/const.py +++ b/homeassistant/components/faa_delays/const.py @@ -1,28 +1,34 @@ """Constants for the FAA Delays integration.""" +from __future__ import annotations -from homeassistant.const import ATTR_ICON, ATTR_NAME +from homeassistant.components.binary_sensor import BinarySensorEntityDescription DOMAIN = "faa_delays" -FAA_BINARY_SENSORS = { - "GROUND_DELAY": { - ATTR_NAME: "Ground Delay", - ATTR_ICON: "mdi:airport", - }, - "GROUND_STOP": { - ATTR_NAME: "Ground Stop", - ATTR_ICON: "mdi:airport", - }, - "DEPART_DELAY": { - ATTR_NAME: "Departure Delay", - ATTR_ICON: "mdi:airplane-takeoff", - }, - "ARRIVE_DELAY": { - ATTR_NAME: "Arrival Delay", - ATTR_ICON: "mdi:airplane-landing", - }, - "CLOSURE": { - ATTR_NAME: "Closure", - ATTR_ICON: "mdi:airplane:off", - }, -} +FAA_BINARY_SENSORS: tuple[BinarySensorEntityDescription, ...] = ( + BinarySensorEntityDescription( + key="GROUND_DELAY", + name="Ground Delay", + icon="mdi:airport", + ), + BinarySensorEntityDescription( + key="GROUND_STOP", + name="Ground Stop", + icon="mdi:airport", + ), + BinarySensorEntityDescription( + key="DEPART_DELAY", + name="Departure Delay", + icon="mdi:airplane-takeoff", + ), + BinarySensorEntityDescription( + key="ARRIVE_DELAY", + name="Arrival Delay", + icon="mdi:airplane-landing", + ), + BinarySensorEntityDescription( + key="CLOSURE", + name="Closure", + icon="mdi:airplane:off", + ), +)