Skip to content

Commit

Permalink
Update custom tempate sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Jan 15, 2024
1 parent 43f53fd commit aa930b8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 20 deletions.
22 changes: 8 additions & 14 deletions custom_components/yandex_station/climate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
HVACMode,
)
from homeassistant.const import CONF_INCLUDE, UnitOfTemperature
from homeassistant.helpers.template import Template

from .core import utils
from .core.const import DATA_CONFIG, DOMAIN
Expand Down Expand Up @@ -47,8 +46,6 @@ class YandexClimate(ClimateEntity, YandexEntity):
preset_instance: str = None
on_value: bool = None
hvac_value: str = None
temperature_template: Template = None
humidity_template: Template = None

def __init__(self, quasar: YandexQuasar, device: dict, config: dict):
super().__init__(quasar, device)
Expand Down Expand Up @@ -104,7 +101,7 @@ def internal_init(self, capabilities: dict, properties: dict):

def internal_update(self, capabilities: dict, properties: dict):
if "on" in capabilities:
self.on_value = capabilities['on']
self.on_value = capabilities["on"]
if self.hvac_instance in capabilities:
self.hvac_value = capabilities[self.hvac_instance]

Expand All @@ -127,22 +124,19 @@ def internal_update(self, capabilities: dict, properties: dict):
if "temperature" in capabilities:
self._attr_target_temperature = capabilities["temperature"]

if self.temperature_template:
self._attr_current_temperature = self.temperature_template.async_render()
elif "temperature" in properties:
if "temperature" in properties:
self._attr_current_temperature = properties["temperature"]

if self.humidity_template:
self._attr_current_humidity = self.humidity_template.async_render()
elif "humidity" in properties:
if "humidity" in properties:
self._attr_current_humidity = properties["humidity"]

async def async_added_to_hass(self):
if item := self.config.get("current_temperature"):
self.temperature_template = Template(item, self.hass)
on_remove = utils.track_template(self.hass, item, self.on_track_template)
self.async_on_remove(on_remove)

if item := self.config.get("current_humidity"):
self.humidity_template = Template(item, self.hass)
def on_track_template(self, value):
self._attr_current_temperature = value
self._async_write_ha_state()

async def async_set_hvac_mode(self, hvac_mode: HVACMode):
if hvac_mode == HVACMode.OFF:
Expand Down
21 changes: 20 additions & 1 deletion custom_components/yandex_station/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import uuid
from datetime import datetime
from logging import Logger
from typing import List
from typing import Callable, List

from aiohttp import ClientSession, web
from homeassistant.components import frontend
Expand All @@ -16,6 +16,12 @@
from homeassistant.helpers import network
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.event import (
async_track_template_result,
TrackTemplate,
TrackTemplateResult,
)
from homeassistant.helpers.template import Template
from homeassistant.helpers.typing import HomeAssistantType
from yarl import URL

Expand Down Expand Up @@ -443,6 +449,19 @@ def instance_include(instance: dict, include: list[str], types: list[str]) -> bo
return instance["parameters"].get("instance", "on") in include


def track_template(hass: HomeAssistant, template: str, update: Callable) -> Callable:
template = Template(template, hass)
update(template.async_render())

def action(event, updates: list[TrackTemplateResult]):
update(next(i.result for i in updates))

track = async_track_template_result(
hass, [TrackTemplate(template=template, variables=None)], action
)
return track.async_remove


class StreamingView(HomeAssistantView):
requires_auth = False

Expand Down
13 changes: 8 additions & 5 deletions custom_components/yandex_station/humidifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
HumidifierEntityFeature,
)
from homeassistant.const import CONF_INCLUDE
from homeassistant.helpers.event import async_track_template_result, TrackTemplate
from homeassistant.helpers.template import Template

from .core import utils
Expand All @@ -30,7 +31,6 @@ async def async_setup_entry(hass, entry, async_add_entities):

# noinspection PyAbstractClass
class YandexHumidifier(HumidifierEntity, YandexEntity):
humidity_template: Template = None
mode_instance: str = None

def __init__(self, quasar: YandexQuasar, device: dict, config: dict):
Expand Down Expand Up @@ -60,14 +60,17 @@ def internal_update(self, capabilities: dict, properties: dict):
if self.mode_instance in capabilities:
self._attr_mode = capabilities[self.mode_instance]

if self.humidity_template:
self._attr_current_humidity = self.humidity_template.async_render()
elif "humidity" in properties:
if "humidity" in properties:
self._attr_current_humidity = properties["humidity"]

async def async_added_to_hass(self):
if item := self.config.get("current_humidity"):
self.humidity_template = Template(item, self.hass)
on_remove = utils.track_template(self.hass, item, self.on_track_template)
self.async_on_remove(on_remove)

def on_track_template(self, value):
self._attr_current_humidity = value
self._async_write_ha_state()

async def async_set_humidity(self, humidity: int) -> None:
await self.quasar.device_action(self.device["id"], "humidity", humidity)
Expand Down

0 comments on commit aa930b8

Please sign in to comment.