Use EntityDescription - rainbird (#53560)

* Use EntityDescription - rainbird

* Add additional type hints
This commit is contained in:
Marc Mueller 2021-07-27 18:06:46 +02:00 committed by GitHub
parent 022ba31999
commit 6847c6dbbc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 62 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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()