mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 18:27:09 +00:00
Use NamedTuple - rainbird (#53329)
* Use NamedTuple - rainbird * Apply suggestions from code review Co-authored-by: Franck Nijhof <frenck@frenck.nl> Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
d371ab9deb
commit
f778467d63
@ -1,5 +1,8 @@
|
|||||||
"""Support for Rain Bird Irrigation system LNK WiFi Module."""
|
"""Support for Rain Bird Irrigation system LNK WiFi Module."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
from typing import NamedTuple
|
||||||
|
|
||||||
from pyrainbird import RainbirdController
|
from pyrainbird import RainbirdController
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -26,10 +29,25 @@ DOMAIN = "rainbird"
|
|||||||
|
|
||||||
SENSOR_TYPE_RAINDELAY = "raindelay"
|
SENSOR_TYPE_RAINDELAY = "raindelay"
|
||||||
SENSOR_TYPE_RAINSENSOR = "rainsensor"
|
SENSOR_TYPE_RAINSENSOR = "rainsensor"
|
||||||
# sensor_type [ description, unit, icon ]
|
|
||||||
SENSOR_TYPES = {
|
|
||||||
SENSOR_TYPE_RAINSENSOR: ["Rainsensor", None, "mdi:water"],
|
class RainBirdSensorMetadata(NamedTuple):
|
||||||
SENSOR_TYPE_RAINDELAY: ["Raindelay", None, "mdi:water-off"],
|
"""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(
|
TRIGGER_TIME_SCHEMA = vol.All(
|
||||||
|
@ -11,6 +11,7 @@ from . import (
|
|||||||
SENSOR_TYPE_RAINDELAY,
|
SENSOR_TYPE_RAINDELAY,
|
||||||
SENSOR_TYPE_RAINSENSOR,
|
SENSOR_TYPE_RAINSENSOR,
|
||||||
SENSOR_TYPES,
|
SENSOR_TYPES,
|
||||||
|
RainBirdSensorMetadata,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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]]
|
controller = hass.data[DATA_RAINBIRD][discovery_info[RAINBIRD_CONTROLLER]]
|
||||||
add_entities(
|
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):
|
class RainBirdSensor(BinarySensorEntity):
|
||||||
"""A sensor implementation for Rain Bird device."""
|
"""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."""
|
"""Initialize the Rain Bird sensor."""
|
||||||
self._sensor_type = sensor_type
|
self._sensor_type = sensor_type
|
||||||
self._controller = controller
|
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
|
self._state = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -45,20 +56,10 @@ class RainBirdSensor(BinarySensorEntity):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data and updates the states."""
|
"""Get the latest data and updates the states."""
|
||||||
_LOGGER.debug("Updating sensor: %s", self._name)
|
_LOGGER.debug("Updating sensor: %s", self.name)
|
||||||
state = None
|
state = None
|
||||||
if self._sensor_type == SENSOR_TYPE_RAINSENSOR:
|
if self._sensor_type == SENSOR_TYPE_RAINSENSOR:
|
||||||
state = self._controller.get_rain_sensor_state()
|
state = self._controller.get_rain_sensor_state()
|
||||||
elif self._sensor_type == SENSOR_TYPE_RAINDELAY:
|
elif self._sensor_type == SENSOR_TYPE_RAINDELAY:
|
||||||
state = self._controller.get_rain_delay()
|
state = self._controller.get_rain_delay()
|
||||||
self._state = None if state is None else bool(state)
|
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
|
|
||||||
|
@ -11,6 +11,7 @@ from . import (
|
|||||||
SENSOR_TYPE_RAINDELAY,
|
SENSOR_TYPE_RAINDELAY,
|
||||||
SENSOR_TYPE_RAINSENSOR,
|
SENSOR_TYPE_RAINSENSOR,
|
||||||
SENSOR_TYPES,
|
SENSOR_TYPES,
|
||||||
|
RainBirdSensorMetadata,
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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]]
|
controller = hass.data[DATA_RAINBIRD][discovery_info[RAINBIRD_CONTROLLER]]
|
||||||
add_entities(
|
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):
|
class RainBirdSensor(SensorEntity):
|
||||||
"""A sensor implementation for Rain Bird device."""
|
"""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."""
|
"""Initialize the Rain Bird sensor."""
|
||||||
self._sensor_type = sensor_type
|
self._sensor_type = sensor_type
|
||||||
self._controller = controller
|
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._unit_of_measurement = SENSOR_TYPES[self._sensor_type][1]
|
self._attr_icon = metadata.icon
|
||||||
|
self._attr_unit_of_measurement = metadata.unit_of_measurement
|
||||||
self._state = None
|
self._state = None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@ -47,23 +58,8 @@ class RainBirdSensor(SensorEntity):
|
|||||||
|
|
||||||
def update(self):
|
def update(self):
|
||||||
"""Get the latest data and updates the states."""
|
"""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:
|
if self._sensor_type == SENSOR_TYPE_RAINSENSOR:
|
||||||
self._state = self._controller.get_rain_sensor_state()
|
self._state = self._controller.get_rain_sensor_state()
|
||||||
elif self._sensor_type == SENSOR_TYPE_RAINDELAY:
|
elif self._sensor_type == SENSOR_TYPE_RAINDELAY:
|
||||||
self._state = self._controller.get_rain_delay()
|
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
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user