Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Finish EntityDescription implementation for RainMachine #55180

Merged
merged 5 commits into from
Aug 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ omit =
homeassistant/components/raincloud/*
homeassistant/components/rainmachine/__init__.py
homeassistant/components/rainmachine/binary_sensor.py
homeassistant/components/rainmachine/model.py
homeassistant/components/rainmachine/sensor.py
homeassistant/components/rainmachine/switch.py
homeassistant/components/raspihats/*
Expand Down
7 changes: 4 additions & 3 deletions homeassistant/components/rainmachine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers import aiohttp_client, config_validation as cv
import homeassistant.helpers.device_registry as dr
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
Expand Down Expand Up @@ -180,7 +181,7 @@ def __init__(
self,
coordinator: DataUpdateCoordinator,
controller: Controller,
entity_type: str,
description: EntityDescription,
) -> None:
"""Initialize."""
super().__init__(coordinator)
Expand All @@ -200,9 +201,9 @@ def __init__(
# The colons are removed from the device MAC simply because that value
# (unnecessarily) makes up the existing unique ID formula and we want to avoid
# a breaking change:
self._attr_unique_id = f"{controller.mac.replace(':', '')}_{entity_type}"
self._attr_unique_id = f"{controller.mac.replace(':', '')}_{description.key}"
self._controller = controller
self._entity_type = entity_type
self.entity_description = description

@callback
def _handle_coordinator_update(self) -> None:
Expand Down
161 changes: 83 additions & 78 deletions homeassistant/components/rainmachine/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""This platform provides binary sensors for key RainMachine data."""
from dataclasses import dataclass
from functools import partial

from regenmaschine.controller import Controller

from homeassistant.components.binary_sensor import BinarySensorEntity
from homeassistant.components.binary_sensor import (
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator

from . import RainMachineEntity
from .const import (
Expand All @@ -18,6 +19,7 @@
DATA_RESTRICTIONS_UNIVERSAL,
DOMAIN,
)
from .model import RainMachineSensorDescriptionMixin

TYPE_FLOW_SENSOR = "flow_sensor"
TYPE_FREEZE = "freeze"
Expand All @@ -29,47 +31,75 @@
TYPE_RAINSENSOR = "rainsensor"
TYPE_WEEKDAY = "weekday"

BINARY_SENSORS = {
TYPE_FLOW_SENSOR: ("Flow Sensor", "mdi:water-pump", True, DATA_PROVISION_SETTINGS),
TYPE_FREEZE: ("Freeze Restrictions", "mdi:cancel", True, DATA_RESTRICTIONS_CURRENT),
TYPE_FREEZE_PROTECTION: (
"Freeze Protection",
"mdi:weather-snowy",
True,
DATA_RESTRICTIONS_UNIVERSAL,

@dataclass
class RainMachineBinarySensorDescription(
BinarySensorEntityDescription, RainMachineSensorDescriptionMixin
):
"""Describe a RainMachine binary sensor."""


BINARY_SENSOR_DESCRIPTIONS = (
RainMachineBinarySensorDescription(
key=TYPE_FLOW_SENSOR,
name="Flow Sensor",
icon="mdi:water-pump",
api_category=DATA_PROVISION_SETTINGS,
),
RainMachineBinarySensorDescription(
key=TYPE_FREEZE,
name="Freeze Restrictions",
icon="mdi:cancel",
api_category=DATA_RESTRICTIONS_CURRENT,
),
RainMachineBinarySensorDescription(
key=TYPE_FREEZE_PROTECTION,
name="Freeze Protection",
icon="mdi:weather-snowy",
api_category=DATA_RESTRICTIONS_UNIVERSAL,
),
TYPE_HOT_DAYS: (
"Extra Water on Hot Days",
"mdi:thermometer-lines",
True,
DATA_RESTRICTIONS_UNIVERSAL,
RainMachineBinarySensorDescription(
key=TYPE_HOT_DAYS,
name="Extra Water on Hot Days",
icon="mdi:thermometer-lines",
api_category=DATA_RESTRICTIONS_UNIVERSAL,
),
TYPE_HOURLY: (
"Hourly Restrictions",
"mdi:cancel",
False,
DATA_RESTRICTIONS_CURRENT,
RainMachineBinarySensorDescription(
key=TYPE_HOURLY,
name="Hourly Restrictions",
icon="mdi:cancel",
entity_registry_enabled_default=False,
api_category=DATA_RESTRICTIONS_CURRENT,
),
TYPE_MONTH: ("Month Restrictions", "mdi:cancel", False, DATA_RESTRICTIONS_CURRENT),
TYPE_RAINDELAY: (
"Rain Delay Restrictions",
"mdi:cancel",
False,
DATA_RESTRICTIONS_CURRENT,
RainMachineBinarySensorDescription(
key=TYPE_MONTH,
name="Month Restrictions",
icon="mdi:cancel",
entity_registry_enabled_default=False,
api_category=DATA_RESTRICTIONS_CURRENT,
),
TYPE_RAINSENSOR: (
"Rain Sensor Restrictions",
"mdi:cancel",
False,
DATA_RESTRICTIONS_CURRENT,
RainMachineBinarySensorDescription(
key=TYPE_RAINDELAY,
name="Rain Delay Restrictions",
icon="mdi:cancel",
entity_registry_enabled_default=False,
api_category=DATA_RESTRICTIONS_CURRENT,
),
TYPE_WEEKDAY: (
"Weekday Restrictions",
"mdi:cancel",
False,
DATA_RESTRICTIONS_CURRENT,
RainMachineBinarySensorDescription(
key=TYPE_RAINSENSOR,
name="Rain Sensor Restrictions",
icon="mdi:cancel",
entity_registry_enabled_default=False,
api_category=DATA_RESTRICTIONS_CURRENT,
),
}
RainMachineBinarySensorDescription(
key=TYPE_WEEKDAY,
name="Weekday Restrictions",
icon="mdi:cancel",
entity_registry_enabled_default=False,
api_category=DATA_RESTRICTIONS_CURRENT,
),
)


async def async_setup_entry(
Expand Down Expand Up @@ -101,74 +131,49 @@ def async_get_sensor(api_category: str) -> partial:

async_add_entities(
[
async_get_sensor(api_category)(
controller, sensor_type, name, icon, enabled_by_default
)
for (
sensor_type,
(name, icon, enabled_by_default, api_category),
) in BINARY_SENSORS.items()
async_get_sensor(description.api_category)(controller, description)
for description in BINARY_SENSOR_DESCRIPTIONS
]
)


class RainMachineBinarySensor(RainMachineEntity, BinarySensorEntity):
"""Define a general RainMachine binary sensor."""

def __init__(
self,
coordinator: DataUpdateCoordinator,
controller: Controller,
sensor_type: str,
name: str,
icon: str,
enabled_by_default: bool,
) -> None:
"""Initialize the sensor."""
super().__init__(coordinator, controller, sensor_type)

self._attr_entity_registry_enabled_default = enabled_by_default
self._attr_icon = icon
self._attr_name = name


class CurrentRestrictionsBinarySensor(RainMachineBinarySensor):
class CurrentRestrictionsBinarySensor(RainMachineEntity, BinarySensorEntity):
"""Define a binary sensor that handles current restrictions data."""

@callback
def update_from_latest_data(self) -> None:
"""Update the state."""
if self._entity_type == TYPE_FREEZE:
if self.entity_description.key == TYPE_FREEZE:
self._attr_is_on = self.coordinator.data["freeze"]
elif self._entity_type == TYPE_HOURLY:
elif self.entity_description.key == TYPE_HOURLY:
self._attr_is_on = self.coordinator.data["hourly"]
elif self._entity_type == TYPE_MONTH:
elif self.entity_description.key == TYPE_MONTH:
self._attr_is_on = self.coordinator.data["month"]
elif self._entity_type == TYPE_RAINDELAY:
elif self.entity_description.key == TYPE_RAINDELAY:
self._attr_is_on = self.coordinator.data["rainDelay"]
elif self._entity_type == TYPE_RAINSENSOR:
elif self.entity_description.key == TYPE_RAINSENSOR:
self._attr_is_on = self.coordinator.data["rainSensor"]
elif self._entity_type == TYPE_WEEKDAY:
elif self.entity_description.key == TYPE_WEEKDAY:
self._attr_is_on = self.coordinator.data["weekDay"]


class ProvisionSettingsBinarySensor(RainMachineBinarySensor):
class ProvisionSettingsBinarySensor(RainMachineEntity, BinarySensorEntity):
"""Define a binary sensor that handles provisioning data."""

@callback
def update_from_latest_data(self) -> None:
"""Update the state."""
if self._entity_type == TYPE_FLOW_SENSOR:
if self.entity_description.key == TYPE_FLOW_SENSOR:
self._attr_is_on = self.coordinator.data["system"].get("useFlowSensor")


class UniversalRestrictionsBinarySensor(RainMachineBinarySensor):
class UniversalRestrictionsBinarySensor(RainMachineEntity, BinarySensorEntity):
"""Define a binary sensor that handles universal restrictions data."""

@callback
def update_from_latest_data(self) -> None:
"""Update the state."""
if self._entity_type == TYPE_FREEZE_PROTECTION:
if self.entity_description.key == TYPE_FREEZE_PROTECTION:
self._attr_is_on = self.coordinator.data["freezeProtectEnabled"]
elif self._entity_type == TYPE_HOT_DAYS:
elif self.entity_description.key == TYPE_HOT_DAYS:
self._attr_is_on = self.coordinator.data["hotDaysExtraWatering"]
9 changes: 9 additions & 0 deletions homeassistant/components/rainmachine/model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Define RainMachine data models."""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file is new and needs to be excluded in.coveragerc

from dataclasses import dataclass


@dataclass
class RainMachineSensorDescriptionMixin:
"""Define an entity description mixin for binary and regular sensors."""

api_category: str
Loading