Skip to content

Commit

Permalink
Add binary sensors for boosts
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-codechimp committed Jan 7, 2025
1 parent 6302059 commit af18a4f
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 2 deletions.
2 changes: 1 addition & 1 deletion custom_components/hive_local_thermostat/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
)

PLATFORMS: list[Platform] = [
Platform.SENSOR, Platform.CLIMATE, Platform.NUMBER, Platform.SELECT, Platform.BUTTON,
Platform.SENSOR, Platform.CLIMATE, Platform.NUMBER, Platform.SELECT, Platform.BUTTON, Platform.BINARY_SENSOR
]

CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN)
Expand Down
134 changes: 134 additions & 0 deletions custom_components/hive_local_thermostat/binary_sensor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
"""Binary Sensor platform for hive_local_thermostat."""

from __future__ import annotations

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

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

from .const import (
CONF_MODEL,
CONF_MQTT_TOPIC,
DOMAIN,
MODEL_SLR2,
)
from .entity import HiveEntity, HiveEntityDescription


@dataclass(frozen=True, kw_only=True)
class HiveBinarySensorEntityDescription(
HiveEntityDescription,
BinarySensorEntityDescription,
):
"""Class describing Hive binary sensor entities."""

value_fn: Callable[[dict[str, Any]], bool | None]
running_state: bool = False


async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the binary sensor platform."""

if config_entry.options[CONF_MODEL] == MODEL_SLR2:
entity_descriptions = [
HiveBinarySensorEntityDescription(
key="heat_boost",
translation_key="heat_boost",
icon="mdi:rocket-launch",
name=config_entry.title,
value_fn=lambda js: js["system_mode_heat"]=="emergency_heating",
topic=config_entry.options[CONF_MQTT_TOPIC],
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
running_state=True,
),
HiveBinarySensorEntityDescription(
key="water_boost",
translation_key="water_boost",
icon="mdi:rocket-launch",
name=config_entry.title,
value_fn=lambda js: js["system_mode_water"]=="emergency_heating",
topic=config_entry.options[CONF_MQTT_TOPIC],
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
running_state=True,
),
]
else:
entity_descriptions = [
HiveBinarySensorEntityDescription(
key="heat_boost",
translation_key="heat_boost",
icon="mdi:rocket-launch",
name=config_entry.title,
value_fn=lambda js: js["system_mode_heat"]=="emergency_heating",
topic=config_entry.options[CONF_MQTT_TOPIC],
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
running_state=True,
),
]

_entities = [
HiveBinarySensor(
entity_description=entity_description,
)
for entity_description in entity_descriptions
]

async_add_entities(sensorEntity for sensorEntity in _entities)

hass.data[DOMAIN][config_entry.entry_id][Platform.BINARY_SENSOR] = _entities


class HiveBinarySensor(HiveEntity, BinarySensorEntity):
"""hive_local_thermostat Binary Sensor class."""

entity_description: HiveBinarySensorEntityDescription

def __init__(
self,
entity_description: HiveBinarySensorEntityDescription,
) -> None:
"""Initialize the binary sensor class."""

self.entity_description = entity_description
self._attr_unique_id = (
f"{DOMAIN}_{entity_description.name}_{entity_description.key}".lower()
)
self._attr_has_entity_name = True
self._func = entity_description.value_fn
self._topic = entity_description.topic

super().__init__(entity_description)

def process_update(self, mqtt_data) -> None:
"""Update the state of the sensor."""

try:
new_value = self._func(mqtt_data)
except KeyError:
new_value = None

self._attr_is_on = new_value

if (
self.hass is not None
): # this is a hack to get around the fact that the entity is not yet initialized at first
self.async_schedule_update_ha_state()

1 change: 0 additions & 1 deletion custom_components/hive_local_thermostat/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ class HiveSensorEntityDescription(

icons_by_state: dict[str, str] | None = None
value_fn: Callable[[dict[str, Any]], str | int | float | None]
# value_fn: any | None = None
running_state: bool = False


Expand Down
8 changes: 8 additions & 0 deletions custom_components/hive_local_thermostat/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@
}
},
"entity": {
"binary_sensor": {
"heat_boost": {
"name": "Heating boost"
},
"water_boost": {
"name": "Water boost"
}
},
"sensor": {
"local_temperature_heat": {
"name": "Current temperature"
Expand Down

0 comments on commit af18a4f

Please sign in to comment.