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

Boost sensors #67

Merged
merged 4 commits into from
Jan 7, 2025
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
2 changes: 1 addition & 1 deletion .devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "andrew-codechimp/hive-local-thermostat",
"image": "mcr.microsoft.com/vscode/devcontainers/python:3.12",
"image": "mcr.microsoft.com/devcontainers/python:dev-3.13",
"features": {
"ghcr.io/devcontainers/features/github-cli:1": {
"installDirectlyFromGitHubRelease": true,
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
runs-on: "ubuntu-latest"
strategy:
matrix:
python-version: ["3.12"]
python-version: ["3.13"]
steps:
- name: Checkout repo
uses: actions/checkout@v4
Expand Down
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()

2 changes: 1 addition & 1 deletion custom_components/hive_local_thermostat/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

LOGGER: Logger = getLogger(__package__)

MIN_HA_VERSION = "2024.7"
MIN_HA_VERSION = "2024.12"

manifestfile = Path(__file__).parent / "manifest.json"
with open(file=manifestfile, encoding="UTF-8") as json_file:
Expand Down
32 changes: 31 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 Expand Up @@ -99,6 +98,26 @@ async def async_setup_entry(
model=config_entry.options[CONF_MODEL],
running_state=True,
),
HiveSensorEntityDescription(
key="boost_remaining_heat",
translation_key="boost_remaining_heat",
icon="mdi:timer-outline",
name=config_entry.title,
value_fn=lambda data: cast(int, data["temperature_setpoint_hold_duration_heat"] if data["system_mode_heat"] == "emergency_heating" else None),
topic=config_entry.options[CONF_MQTT_TOPIC],
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
),
HiveSensorEntityDescription(
key="boost_remaining_water",
translation_key="boost_remaining_water",
icon="mdi:timer-outline",
name=config_entry.title,
value_fn=lambda data: cast(int, data["temperature_setpoint_hold_duration_water"] if data["system_mode_water"] == "emergency_heating" else None),
topic=config_entry.options[CONF_MQTT_TOPIC],
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
),
]
else:
entity_descriptions = [
Expand Down Expand Up @@ -132,6 +151,17 @@ async def async_setup_entry(
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
),
HiveSensorEntityDescription(
key="boost_remaining_heat",
translation_key="boost_remaining_heat",
icon="mdi:timer-outline",
name=config_entry.title,
suggested_display_precision=1,
value_fn=lambda data: cast(int, data["temperature_setpoint_hold_duration"] if data["system_mode"] == "emergency_heating" else None),
topic=config_entry.options[CONF_MQTT_TOPIC],
entry_id=config_entry.entry_id,
model=config_entry.options[CONF_MODEL],
),
]

_entities = [
Expand Down
16 changes: 16 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 All @@ -61,6 +69,14 @@
"heat": "Heating",
"preheating": "Preheating"
}
},
"boost_remaining_heat": {
"name": "Heating boost remaining",
"unit_of_measurement": "minutes"
},
"boost_remaining_water": {
"name": "Water boost remaining",
"unit_of_measurement": "minutes"
}
},
"climate": {
Expand Down
2 changes: 1 addition & 1 deletion hacs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "Hive Local Thermostat",
"filename": "hive_local_thermostat.zip",
"hide_default_branch": true,
"homeassistant": "2024.7.0",
"homeassistant": "2024.12.0",
"render_readme": true,
"zip_release": true
}
Loading
Loading