diff --git a/homeassistant/components/rainbird/__init__.py b/homeassistant/components/rainbird/__init__.py index 55ed421bd24..6aa6e837abe 100644 --- a/homeassistant/components/rainbird/__init__.py +++ b/homeassistant/components/rainbird/__init__.py @@ -2,12 +2,13 @@ from __future__ import annotations import logging -from typing import NamedTuple from pyrainbird import RainbirdController import voluptuous as vol from homeassistant.components import binary_sensor, sensor, switch +from homeassistant.components.binary_sensor import BinarySensorEntityDescription +from homeassistant.components.sensor import SensorEntityDescription from homeassistant.const import ( CONF_FRIENDLY_NAME, CONF_HOST, @@ -31,24 +32,31 @@ SENSOR_TYPE_RAINDELAY = "raindelay" SENSOR_TYPE_RAINSENSOR = "rainsensor" -class RainBirdSensorMetadata(NamedTuple): - """Metadata for an individual RainBird sensor.""" - - name: str - icon: str - unit_of_measurement: str | None = None - - -SENSOR_TYPES: dict[str, RainBirdSensorMetadata] = { - SENSOR_TYPE_RAINSENSOR: RainBirdSensorMetadata( - "Rainsensor", +SENSOR_TYPES: tuple[SensorEntityDescription, ...] = ( + SensorEntityDescription( + key=SENSOR_TYPE_RAINSENSOR, + name="Rainsensor", icon="mdi:water", ), - SENSOR_TYPE_RAINDELAY: RainBirdSensorMetadata( - "Raindelay", + SensorEntityDescription( + key=SENSOR_TYPE_RAINDELAY, + name="Raindelay", icon="mdi:water-off", ), -} +) + +BINARY_SENSOR_TYPES: tuple[BinarySensorEntityDescription, ...] = ( + BinarySensorEntityDescription( + key=SENSOR_TYPE_RAINSENSOR, + name="Rainsensor", + icon="mdi:water", + ), + BinarySensorEntityDescription( + key=SENSOR_TYPE_RAINDELAY, + name="Raindelay", + icon="mdi:water-off", + ), +) TRIGGER_TIME_SCHEMA = vol.All( cv.time_period, cv.positive_timedelta, lambda td: (td.total_seconds() // 60) diff --git a/homeassistant/components/rainbird/binary_sensor.py b/homeassistant/components/rainbird/binary_sensor.py index 9960d7670b2..476c2cfc8a2 100644 --- a/homeassistant/components/rainbird/binary_sensor.py +++ b/homeassistant/components/rainbird/binary_sensor.py @@ -3,15 +3,17 @@ import logging from pyrainbird import RainbirdController -from homeassistant.components.binary_sensor import BinarySensorEntity +from homeassistant.components.binary_sensor import ( + BinarySensorEntity, + BinarySensorEntityDescription, +) from . import ( + BINARY_SENSOR_TYPES, DATA_RAINBIRD, RAINBIRD_CONTROLLER, SENSOR_TYPE_RAINDELAY, SENSOR_TYPE_RAINSENSOR, - SENSOR_TYPES, - RainBirdSensorMetadata, ) _LOGGER = logging.getLogger(__name__) @@ -25,8 +27,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None): controller = hass.data[DATA_RAINBIRD][discovery_info[RAINBIRD_CONTROLLER]] add_entities( [ - RainBirdSensor(controller, sensor_type, metadata) - for sensor_type, metadata in SENSOR_TYPES.items() + RainBirdSensor(controller, description) + for description in BINARY_SENSOR_TYPES ], True, ) @@ -38,28 +40,18 @@ class RainBirdSensor(BinarySensorEntity): def __init__( self, controller: RainbirdController, - sensor_type, - metadata: RainBirdSensorMetadata, - ): + description: BinarySensorEntityDescription, + ) -> None: """Initialize the Rain Bird sensor.""" - self._sensor_type = sensor_type + self.entity_description = description self._controller = controller - self._attr_name = metadata.name - self._attr_icon = metadata.icon - self._state = None - - @property - def is_on(self): - """Return true if the binary sensor is on.""" - return None if self._state is None else bool(self._state) - - def update(self): + def update(self) -> None: """Get the latest data and updates the states.""" _LOGGER.debug("Updating sensor: %s", self.name) state = None - if self._sensor_type == SENSOR_TYPE_RAINSENSOR: + if self.entity_description.key == SENSOR_TYPE_RAINSENSOR: state = self._controller.get_rain_sensor_state() - elif self._sensor_type == SENSOR_TYPE_RAINDELAY: + elif self.entity_description.key == SENSOR_TYPE_RAINDELAY: state = self._controller.get_rain_delay() - self._state = None if state is None else bool(state) + self._attr_is_on = None if state is None else bool(state) diff --git a/homeassistant/components/rainbird/sensor.py b/homeassistant/components/rainbird/sensor.py index 36c3d50e1c1..2158bc5cf97 100644 --- a/homeassistant/components/rainbird/sensor.py +++ b/homeassistant/components/rainbird/sensor.py @@ -3,7 +3,7 @@ import logging from pyrainbird import RainbirdController -from homeassistant.components.sensor import SensorEntity +from homeassistant.components.sensor import SensorEntity, SensorEntityDescription from . import ( DATA_RAINBIRD, @@ -11,7 +11,6 @@ from . import ( SENSOR_TYPE_RAINDELAY, SENSOR_TYPE_RAINSENSOR, SENSOR_TYPES, - RainBirdSensorMetadata, ) _LOGGER = logging.getLogger(__name__) @@ -25,10 +24,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None): controller = hass.data[DATA_RAINBIRD][discovery_info[RAINBIRD_CONTROLLER]] add_entities( - [ - RainBirdSensor(controller, sensor_type, metadata) - for sensor_type, metadata in SENSOR_TYPES.items() - ], + [RainBirdSensor(controller, description) for description in SENSOR_TYPES], True, ) @@ -39,27 +35,16 @@ class RainBirdSensor(SensorEntity): def __init__( self, controller: RainbirdController, - sensor_type, - metadata: RainBirdSensorMetadata, - ): + description: SensorEntityDescription, + ) -> None: """Initialize the Rain Bird sensor.""" - self._sensor_type = sensor_type + self.entity_description = description self._controller = controller - self._attr_name = metadata.name - self._attr_icon = metadata.icon - self._attr_unit_of_measurement = metadata.unit_of_measurement - self._state = None - - @property - def state(self): - """Return the state of the sensor.""" - return self._state - - def update(self): + def update(self) -> None: """Get the latest data and updates the states.""" _LOGGER.debug("Updating sensor: %s", self.name) - if self._sensor_type == SENSOR_TYPE_RAINSENSOR: - self._state = self._controller.get_rain_sensor_state() - elif self._sensor_type == SENSOR_TYPE_RAINDELAY: - self._state = self._controller.get_rain_delay() + if self.entity_description.key == SENSOR_TYPE_RAINSENSOR: + self._attr_state = self._controller.get_rain_sensor_state() + elif self.entity_description.key == SENSOR_TYPE_RAINDELAY: + self._attr_state = self._controller.get_rain_delay()