2019-04-03 15:40:03 +00:00
|
|
|
"""Support for Rain Bird Irrigation system LNK WiFi Module."""
|
2022-01-03 18:08:34 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2017-12-25 09:07:18 +00:00
|
|
|
import logging
|
|
|
|
|
2019-09-26 09:24:03 +00:00
|
|
|
from pyrainbird import RainbirdController
|
2017-12-25 09:07:18 +00:00
|
|
|
|
2021-07-27 16:06:46 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
|
2022-01-03 18:08:34 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2017-12-25 09:07:18 +00:00
|
|
|
|
2019-09-26 09:24:03 +00:00
|
|
|
from . import (
|
|
|
|
DATA_RAINBIRD,
|
|
|
|
RAINBIRD_CONTROLLER,
|
|
|
|
SENSOR_TYPE_RAINDELAY,
|
|
|
|
SENSOR_TYPE_RAINSENSOR,
|
|
|
|
SENSOR_TYPES,
|
|
|
|
)
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2017-12-25 09:07:18 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-01-03 18:08:34 +00:00
|
|
|
def setup_platform(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
add_entities: AddEntitiesCallback,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> None:
|
2017-12-25 09:07:18 +00:00
|
|
|
"""Set up a Rain Bird sensor."""
|
|
|
|
|
2019-09-26 09:24:03 +00:00
|
|
|
if discovery_info is None:
|
|
|
|
return
|
2017-12-25 09:07:18 +00:00
|
|
|
|
2019-09-26 09:24:03 +00:00
|
|
|
controller = hass.data[DATA_RAINBIRD][discovery_info[RAINBIRD_CONTROLLER]]
|
|
|
|
add_entities(
|
2021-07-27 16:06:46 +00:00
|
|
|
[RainBirdSensor(controller, description) for description in SENSOR_TYPES],
|
2021-07-22 13:29:50 +00:00
|
|
|
True,
|
2019-09-26 09:24:03 +00:00
|
|
|
)
|
2017-12-25 09:07:18 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class RainBirdSensor(SensorEntity):
|
2017-12-25 09:07:18 +00:00
|
|
|
"""A sensor implementation for Rain Bird device."""
|
|
|
|
|
2021-07-22 13:29:50 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
controller: RainbirdController,
|
2021-07-27 16:06:46 +00:00
|
|
|
description: SensorEntityDescription,
|
|
|
|
) -> None:
|
2017-12-25 09:07:18 +00:00
|
|
|
"""Initialize the Rain Bird sensor."""
|
2021-07-27 16:06:46 +00:00
|
|
|
self.entity_description = description
|
2017-12-25 09:07:18 +00:00
|
|
|
self._controller = controller
|
2021-07-22 13:29:50 +00:00
|
|
|
|
2021-07-27 16:06:46 +00:00
|
|
|
def update(self) -> None:
|
2017-12-25 09:07:18 +00:00
|
|
|
"""Get the latest data and updates the states."""
|
2021-07-22 13:29:50 +00:00
|
|
|
_LOGGER.debug("Updating sensor: %s", self.name)
|
2021-07-27 16:06:46 +00:00
|
|
|
if self.entity_description.key == SENSOR_TYPE_RAINSENSOR:
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = self._controller.get_rain_sensor_state()
|
2021-07-27 16:06:46 +00:00
|
|
|
elif self.entity_description.key == SENSOR_TYPE_RAINDELAY:
|
2021-08-12 15:40:55 +00:00
|
|
|
self._attr_native_value = self._controller.get_rain_delay()
|