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

Add OSO Energy binary sensors #117174

Merged
merged 11 commits into from
May 29, 2024
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,7 @@ omit =
homeassistant/components/oru/*
homeassistant/components/orvibo/switch.py
homeassistant/components/osoenergy/__init__.py
homeassistant/components/osoenergy/binary_sensor.py
edenhaus marked this conversation as resolved.
Show resolved Hide resolved
homeassistant/components/osoenergy/const.py
homeassistant/components/osoenergy/sensor.py
homeassistant/components/osoenergy/water_heater.py
Expand Down
2 changes: 2 additions & 0 deletions homeassistant/components/osoenergy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@

MANUFACTURER = "OSO Energy"
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.SENSOR,
Platform.WATER_HEATER,
]
PLATFORM_LOOKUP = {
Platform.BINARY_SENSOR: "binary_sensor",
Platform.SENSOR: "sensor",
Platform.WATER_HEATER: "water_heater",
}
Expand Down
95 changes: 95 additions & 0 deletions homeassistant/components/osoenergy/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""Support for OSO Energy binary sensors."""

from collections.abc import Callable
from dataclasses import dataclass

from apyosoenergyapi import OSOEnergy
from apyosoenergyapi.helper.const import OSOEnergyBinarySensorData

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

from . import OSOEnergyEntity
from .const import DOMAIN


@dataclass(frozen=True, kw_only=True)
class OSOEnergyBinarySensorEntityDescription(BinarySensorEntityDescription):
"""Class describing OSO Energy heater binary sensor entities."""

value_fn: Callable[[OSOEnergy], bool]


SENSOR_TYPES: dict[str, OSOEnergyBinarySensorEntityDescription] = {
"power_save": OSOEnergyBinarySensorEntityDescription(
key="power_save",
translation_key="power_save",
value_fn=lambda entity_data: entity_data.state,
),
"extra_energy": OSOEnergyBinarySensorEntityDescription(
key="extra_energy",
translation_key="extra_energy",
osohotwateriot marked this conversation as resolved.
Show resolved Hide resolved
value_fn=lambda entity_data: entity_data.state,
),
"heater_state": OSOEnergyBinarySensorEntityDescription(
key="heater_state",
translation_key="heater_state",
value_fn=lambda entity_data: entity_data.state,
),
}


async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up OSO Energy binary sensor."""
osoenergy = hass.data[DOMAIN][entry.entry_id]
osohotwateriot marked this conversation as resolved.
Show resolved Hide resolved
devices = osoenergy.session.device_list.get("binary_sensor")
entities = []
if devices:
for dev in devices:
sensor_type = dev.osoEnergyType.lower()
if sensor_type in SENSOR_TYPES:
entities.append(
OSOEnergyBinarySensor(osoenergy, SENSOR_TYPES[sensor_type], dev)
)

osohotwateriot marked this conversation as resolved.
Show resolved Hide resolved
async_add_entities(entities, True)


class OSOEnergyBinarySensor(
OSOEnergyEntity[OSOEnergyBinarySensorData], BinarySensorEntity
):
"""OSO Energy Sensor Entity."""

entity_description: OSOEnergyBinarySensorEntityDescription

def __init__(
self,
instance: OSOEnergy,
description: OSOEnergyBinarySensorEntityDescription,
entity_data: OSOEnergyBinarySensorData,
) -> None:
"""Set up OSO Energy binary sensor."""
super().__init__(instance, entity_data)

device_id = entity_data.device_id
osohotwateriot marked this conversation as resolved.
Show resolved Hide resolved
self._attr_unique_id = f"{device_id}_{description.key}"
self.entity_description = description

@property
def is_on(self) -> bool | None:
"""Return the state of the sensor."""
return self.entity_description.value_fn(self.entity_data)

async def async_update(self) -> None:
"""Update all data for OSO Energy."""
await self.osoenergy.session.update_data()
self.entity_data = await self.osoenergy.binary_sensor.get_sensor(
self.entity_data
)
15 changes: 15 additions & 0 deletions homeassistant/components/osoenergy/icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"entity": {
"binary_sensor": {
"power_save": {
"default": "mdi:power-sleep"
},
"extra_energy": {
"default": "mdi:white-balance-sunny"
},
"heater_state": {
"default": "mdi:water-boiler"
}
}
}
}
11 changes: 11 additions & 0 deletions homeassistant/components/osoenergy/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@
}
},
"entity": {
"binary_sensor": {
"power_save": {
"name": "Power save"
},
"extra_energy": {
"name": "Extra energy"
},
"heater_state": {
"name": "Heater state"
osohotwateriot marked this conversation as resolved.
Show resolved Hide resolved
}
},
"sensor": {
"tapping_capacity": {
"name": "Tapping capacity"
Expand Down