Skip to content

Commit

Permalink
Add binary sensors to fyta (#134900)
Browse files Browse the repository at this point in the history
Co-authored-by: Joost Lekkerkerker <[email protected]>
  • Loading branch information
dontinelli and joostlek authored Jan 8, 2025
1 parent ec7d2f3 commit 99e65c3
Show file tree
Hide file tree
Showing 11 changed files with 1,014 additions and 8 deletions.
1 change: 1 addition & 0 deletions homeassistant/components/fyta/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
_LOGGER = logging.getLogger(__name__)

PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.SENSOR,
]
type FytaConfigEntry = ConfigEntry[FytaCoordinator]
Expand Down
117 changes: 117 additions & 0 deletions homeassistant/components/fyta/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
"""Binary sensors for Fyta."""

from __future__ import annotations

from collections.abc import Callable
from dataclasses import dataclass
from typing import Final

from fyta_cli.fyta_models import Plant

from homeassistant.components.binary_sensor import (
BinarySensorDeviceClass,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from . import FytaConfigEntry
from .entity import FytaPlantEntity


@dataclass(frozen=True, kw_only=True)
class FytaBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Describes Fyta binary sensor entity."""

value_fn: Callable[[Plant], bool]


BINARY_SENSORS: Final[list[FytaBinarySensorEntityDescription]] = [
FytaBinarySensorEntityDescription(
key="low_battery",
device_class=BinarySensorDeviceClass.BATTERY,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda plant: plant.low_battery,
),
FytaBinarySensorEntityDescription(
key="notification_light",
translation_key="notification_light",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda plant: plant.notification_light,
),
FytaBinarySensorEntityDescription(
key="notification_nutrition",
translation_key="notification_nutrition",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda plant: plant.notification_nutrition,
),
FytaBinarySensorEntityDescription(
key="notification_temperature",
translation_key="notification_temperature",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda plant: plant.notification_temperature,
),
FytaBinarySensorEntityDescription(
key="notification_water",
translation_key="notification_water",
entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False,
value_fn=lambda plant: plant.notification_water,
),
FytaBinarySensorEntityDescription(
key="sensor_update_available",
device_class=BinarySensorDeviceClass.UPDATE,
entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda plant: plant.sensor_update_available,
),
FytaBinarySensorEntityDescription(
key="productive_plant",
translation_key="productive_plant",
value_fn=lambda plant: plant.productive_plant,
),
FytaBinarySensorEntityDescription(
key="repotted",
translation_key="repotted",
value_fn=lambda plant: plant.repotted,
),
]


async def async_setup_entry(
hass: HomeAssistant, entry: FytaConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the FYTA binary sensors."""
coordinator = entry.runtime_data

async_add_entities(
FytaPlantBinarySensor(coordinator, entry, sensor, plant_id)
for plant_id in coordinator.fyta.plant_list
for sensor in BINARY_SENSORS
if sensor.key in dir(coordinator.data.get(plant_id))
)

def _async_add_new_device(plant_id: int) -> None:
async_add_entities(
FytaPlantBinarySensor(coordinator, entry, sensor, plant_id)
for sensor in BINARY_SENSORS
if sensor.key in dir(coordinator.data.get(plant_id))
)

coordinator.new_device_callbacks.append(_async_add_new_device)


class FytaPlantBinarySensor(FytaPlantEntity, BinarySensorEntity):
"""Represents a Fyta binary sensor."""

entity_description: FytaBinarySensorEntityDescription

@property
def is_on(self) -> bool:
"""Return value of the binary sensor."""

return self.entity_description.value_fn(self.plant)
4 changes: 2 additions & 2 deletions homeassistant/components/fyta/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from fyta_cli.fyta_models import Plant

from homeassistant.components.sensor import SensorEntityDescription
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import CoordinatorEntity

from . import FytaConfigEntry
Expand All @@ -20,7 +20,7 @@ def __init__(
self,
coordinator: FytaCoordinator,
entry: FytaConfigEntry,
description: SensorEntityDescription,
description: EntityDescription,
plant_id: int,
) -> None:
"""Initialize the Fyta sensor."""
Expand Down
20 changes: 20 additions & 0 deletions homeassistant/components/fyta/icons.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
{
"entity": {
"binary_sensor": {
"notification_light": {
"default": "mdi:lightbulb-alert-outline"
},
"notification_nutrition": {
"default": "mdi:beaker-alert-outline"
},
"notification_temperature": {
"default": "mdi:thermometer-alert"
},
"notification_water": {
"default": "mdi:watering-can-outline"
},
"productive_plant": {
"default": "mdi:fruit-grapes"
},
"repotted": {
"default": "mdi:shovel"
}
},
"sensor": {
"status": {
"default": "mdi:flower"
Expand Down
23 changes: 23 additions & 0 deletions homeassistant/components/fyta/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,29 @@
}
},
"entity": {
"binary_sensor": {
"notification_light": {
"name": "Light notification"
},
"notification_nutrition": {
"name": "Nutrition notification"
},
"notification_temperature": {
"name": "Temperature notification"
},
"notification_water": {
"name": "Water notification"
},
"productive_plant": {
"name": "Productive plant"
},
"repotted": {
"name": "Repotted"
},
"sensor_update_available": {
"name": "Sensor update available"
}
},
"sensor": {
"scientific_name": {
"name": "Scientific name"
Expand Down
7 changes: 5 additions & 2 deletions tests/components/fyta/fixtures/plant_status1.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"battery_level": 80,
"low_battery": true,
"fertilisation": {
"was_repotted": true
},
"low_battery": false,
"last_updated": "2023-01-10 10:10:00",
"light": 2,
"light_status": 3,
Expand All @@ -10,7 +13,7 @@
"moisture_status": 3,
"sensor_available": true,
"sensor_id": "FD:1D:B7:E3:D0:E2",
"sensor_update_available": false,
"sensor_update_available": true,
"sw_version": "1.0",
"status": 1,
"online": true,
Expand Down
3 changes: 3 additions & 0 deletions tests/components/fyta/fixtures/plant_status2.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"battery_level": 80,
"fertilisation": {
"was_repotted": true
},
"low_battery": true,
"last_updated": "2023-01-02 10:10:00",
"light": 2,
Expand Down
3 changes: 3 additions & 0 deletions tests/components/fyta/fixtures/plant_status3.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"battery_level": 80,
"fertilisation": {
"was_repotted": false
},
"low_battery": true,
"last_updated": "2023-01-02 10:10:00",
"light": 2,
Expand Down
Loading

0 comments on commit 99e65c3

Please sign in to comment.