diff --git a/homeassistant/components/rainbird/__init__.py b/homeassistant/components/rainbird/__init__.py index d8334470b60..55ed421bd24 100644 --- a/homeassistant/components/rainbird/__init__.py +++ b/homeassistant/components/rainbird/__init__.py @@ -1,5 +1,8 @@ """Support for Rain Bird Irrigation system LNK WiFi Module.""" +from __future__ import annotations + import logging +from typing import NamedTuple from pyrainbird import RainbirdController import voluptuous as vol @@ -26,10 +29,25 @@ DOMAIN = "rainbird" SENSOR_TYPE_RAINDELAY = "raindelay" SENSOR_TYPE_RAINSENSOR = "rainsensor" -# sensor_type [ description, unit, icon ] -SENSOR_TYPES = { - SENSOR_TYPE_RAINSENSOR: ["Rainsensor", None, "mdi:water"], - SENSOR_TYPE_RAINDELAY: ["Raindelay", None, "mdi:water-off"], + + +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", + icon="mdi:water", + ), + SENSOR_TYPE_RAINDELAY: RainBirdSensorMetadata( + "Raindelay", + icon="mdi:water-off", + ), } TRIGGER_TIME_SCHEMA = vol.All( diff --git a/homeassistant/components/rainbird/binary_sensor.py b/homeassistant/components/rainbird/binary_sensor.py index 62c6824f5e0..9960d7670b2 100644 --- a/homeassistant/components/rainbird/binary_sensor.py +++ b/homeassistant/components/rainbird/binary_sensor.py @@ -11,6 +11,7 @@ from . import ( SENSOR_TYPE_RAINDELAY, SENSOR_TYPE_RAINSENSOR, SENSOR_TYPES, + RainBirdSensorMetadata, ) _LOGGER = logging.getLogger(__name__) @@ -23,19 +24,29 @@ 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) for sensor_type in SENSOR_TYPES], True + [ + RainBirdSensor(controller, sensor_type, metadata) + for sensor_type, metadata in SENSOR_TYPES.items() + ], + True, ) class RainBirdSensor(BinarySensorEntity): """A sensor implementation for Rain Bird device.""" - def __init__(self, controller: RainbirdController, sensor_type): + def __init__( + self, + controller: RainbirdController, + sensor_type, + metadata: RainBirdSensorMetadata, + ): """Initialize the Rain Bird sensor.""" self._sensor_type = sensor_type self._controller = controller - self._name = SENSOR_TYPES[self._sensor_type][0] - self._icon = SENSOR_TYPES[self._sensor_type][2] + + self._attr_name = metadata.name + self._attr_icon = metadata.icon self._state = None @property @@ -45,20 +56,10 @@ class RainBirdSensor(BinarySensorEntity): def update(self): """Get the latest data and updates the states.""" - _LOGGER.debug("Updating sensor: %s", self._name) + _LOGGER.debug("Updating sensor: %s", self.name) state = None if self._sensor_type == SENSOR_TYPE_RAINSENSOR: state = self._controller.get_rain_sensor_state() elif self._sensor_type == SENSOR_TYPE_RAINDELAY: state = self._controller.get_rain_delay() self._state = None if state is None else bool(state) - - @property - def name(self): - """Return the name of this camera.""" - return self._name - - @property - def icon(self): - """Return icon.""" - return self._icon diff --git a/homeassistant/components/rainbird/sensor.py b/homeassistant/components/rainbird/sensor.py index 2c542dc12a9..36c3d50e1c1 100644 --- a/homeassistant/components/rainbird/sensor.py +++ b/homeassistant/components/rainbird/sensor.py @@ -11,6 +11,7 @@ from . import ( SENSOR_TYPE_RAINDELAY, SENSOR_TYPE_RAINSENSOR, SENSOR_TYPES, + RainBirdSensorMetadata, ) _LOGGER = logging.getLogger(__name__) @@ -24,20 +25,30 @@ 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) for sensor_type in SENSOR_TYPES], True + [ + RainBirdSensor(controller, sensor_type, metadata) + for sensor_type, metadata in SENSOR_TYPES.items() + ], + True, ) class RainBirdSensor(SensorEntity): """A sensor implementation for Rain Bird device.""" - def __init__(self, controller: RainbirdController, sensor_type): + def __init__( + self, + controller: RainbirdController, + sensor_type, + metadata: RainBirdSensorMetadata, + ): """Initialize the Rain Bird sensor.""" self._sensor_type = sensor_type self._controller = controller - self._name = SENSOR_TYPES[self._sensor_type][0] - self._icon = SENSOR_TYPES[self._sensor_type][2] - self._unit_of_measurement = SENSOR_TYPES[self._sensor_type][1] + + self._attr_name = metadata.name + self._attr_icon = metadata.icon + self._attr_unit_of_measurement = metadata.unit_of_measurement self._state = None @property @@ -47,23 +58,8 @@ class RainBirdSensor(SensorEntity): def update(self): """Get the latest data and updates the states.""" - _LOGGER.debug("Updating sensor: %s", self._name) + _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() - - @property - def name(self): - """Return the name of this camera.""" - return self._name - - @property - def unit_of_measurement(self): - """Return the units of measurement.""" - return self._unit_of_measurement - - @property - def icon(self): - """Return icon.""" - return self._icon