mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 03:07:37 +00:00
Use EntityDescription - rainbird (#53560)
* Use EntityDescription - rainbird * Add additional type hints
This commit is contained in:
parent
022ba31999
commit
6847c6dbbc
@ -2,12 +2,13 @@
|
|||||||
from __future__ import annotations
|
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
|
||||||
|
|
||||||
from homeassistant.components import binary_sensor, sensor, switch
|
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 (
|
from homeassistant.const import (
|
||||||
CONF_FRIENDLY_NAME,
|
CONF_FRIENDLY_NAME,
|
||||||
CONF_HOST,
|
CONF_HOST,
|
||||||
@ -31,24 +32,31 @@ SENSOR_TYPE_RAINDELAY = "raindelay"
|
|||||||
SENSOR_TYPE_RAINSENSOR = "rainsensor"
|
SENSOR_TYPE_RAINSENSOR = "rainsensor"
|
||||||
|
|
||||||
|
|
||||||
class RainBirdSensorMetadata(NamedTuple):
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"""Metadata for an individual RainBird sensor."""
|
SensorEntityDescription(
|
||||||
|
key=SENSOR_TYPE_RAINSENSOR,
|
||||||
name: str
|
name="Rainsensor",
|
||||||
icon: str
|
|
||||||
unit_of_measurement: str | None = None
|
|
||||||
|
|
||||||
|
|
||||||
SENSOR_TYPES: dict[str, RainBirdSensorMetadata] = {
|
|
||||||
SENSOR_TYPE_RAINSENSOR: RainBirdSensorMetadata(
|
|
||||||
"Rainsensor",
|
|
||||||
icon="mdi:water",
|
icon="mdi:water",
|
||||||
),
|
),
|
||||||
SENSOR_TYPE_RAINDELAY: RainBirdSensorMetadata(
|
SensorEntityDescription(
|
||||||
"Raindelay",
|
key=SENSOR_TYPE_RAINDELAY,
|
||||||
|
name="Raindelay",
|
||||||
icon="mdi:water-off",
|
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(
|
TRIGGER_TIME_SCHEMA = vol.All(
|
||||||
cv.time_period, cv.positive_timedelta, lambda td: (td.total_seconds() // 60)
|
cv.time_period, cv.positive_timedelta, lambda td: (td.total_seconds() // 60)
|
||||||
|
@ -3,15 +3,17 @@ import logging
|
|||||||
|
|
||||||
from pyrainbird import RainbirdController
|
from pyrainbird import RainbirdController
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import BinarySensorEntity
|
from homeassistant.components.binary_sensor import (
|
||||||
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
|
)
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
|
BINARY_SENSOR_TYPES,
|
||||||
DATA_RAINBIRD,
|
DATA_RAINBIRD,
|
||||||
RAINBIRD_CONTROLLER,
|
RAINBIRD_CONTROLLER,
|
||||||
SENSOR_TYPE_RAINDELAY,
|
SENSOR_TYPE_RAINDELAY,
|
||||||
SENSOR_TYPE_RAINSENSOR,
|
SENSOR_TYPE_RAINSENSOR,
|
||||||
SENSOR_TYPES,
|
|
||||||
RainBirdSensorMetadata,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_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]]
|
controller = hass.data[DATA_RAINBIRD][discovery_info[RAINBIRD_CONTROLLER]]
|
||||||
add_entities(
|
add_entities(
|
||||||
[
|
[
|
||||||
RainBirdSensor(controller, sensor_type, metadata)
|
RainBirdSensor(controller, description)
|
||||||
for sensor_type, metadata in SENSOR_TYPES.items()
|
for description in BINARY_SENSOR_TYPES
|
||||||
],
|
],
|
||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
@ -38,28 +40,18 @@ class RainBirdSensor(BinarySensorEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
controller: RainbirdController,
|
controller: RainbirdController,
|
||||||
sensor_type,
|
description: BinarySensorEntityDescription,
|
||||||
metadata: RainBirdSensorMetadata,
|
) -> None:
|
||||||
):
|
|
||||||
"""Initialize the Rain Bird sensor."""
|
"""Initialize the Rain Bird sensor."""
|
||||||
self._sensor_type = sensor_type
|
self.entity_description = description
|
||||||
self._controller = controller
|
self._controller = controller
|
||||||
|
|
||||||
self._attr_name = metadata.name
|
def update(self) -> None:
|
||||||
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):
|
|
||||||
"""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.entity_description.key == 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.entity_description.key == 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._attr_is_on = None if state is None else bool(state)
|
||||||
|
@ -3,7 +3,7 @@ import logging
|
|||||||
|
|
||||||
from pyrainbird import RainbirdController
|
from pyrainbird import RainbirdController
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorEntity
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
||||||
|
|
||||||
from . import (
|
from . import (
|
||||||
DATA_RAINBIRD,
|
DATA_RAINBIRD,
|
||||||
@ -11,7 +11,6 @@ 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__)
|
||||||
@ -25,10 +24,7 @@ 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, description) for description in SENSOR_TYPES],
|
||||||
RainBirdSensor(controller, sensor_type, metadata)
|
|
||||||
for sensor_type, metadata in SENSOR_TYPES.items()
|
|
||||||
],
|
|
||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -39,27 +35,16 @@ class RainBirdSensor(SensorEntity):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
controller: RainbirdController,
|
controller: RainbirdController,
|
||||||
sensor_type,
|
description: SensorEntityDescription,
|
||||||
metadata: RainBirdSensorMetadata,
|
) -> None:
|
||||||
):
|
|
||||||
"""Initialize the Rain Bird sensor."""
|
"""Initialize the Rain Bird sensor."""
|
||||||
self._sensor_type = sensor_type
|
self.entity_description = description
|
||||||
self._controller = controller
|
self._controller = controller
|
||||||
|
|
||||||
self._attr_name = metadata.name
|
def update(self) -> None:
|
||||||
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):
|
|
||||||
"""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.entity_description.key == SENSOR_TYPE_RAINSENSOR:
|
||||||
self._state = self._controller.get_rain_sensor_state()
|
self._attr_state = self._controller.get_rain_sensor_state()
|
||||||
elif self._sensor_type == SENSOR_TYPE_RAINDELAY:
|
elif self.entity_description.key == SENSOR_TYPE_RAINDELAY:
|
||||||
self._state = self._controller.get_rain_delay()
|
self._attr_state = self._controller.get_rain_delay()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user