Skip to content

Commit

Permalink
Added Service to set value
Browse files Browse the repository at this point in the history
  • Loading branch information
zaubererty committed Feb 26, 2022
1 parent c10e3f7 commit 5d31977
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
25 changes: 24 additions & 1 deletion custom_components/4heat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
""" Integration for 4heat"""
import voluptuous as vol
import logging

from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.core import valid_entity_id
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
Expand All @@ -12,9 +14,10 @@
from homeassistant.exceptions import ConfigEntryNotReady
from homeassistant.helpers.typing import HomeAssistantType

from .const import DOMAIN, DATA_COORDINATOR, CONF_MODE
from .const import ATTR_MARKER, ATTR_READING_ID, ATTR_STOVE_ID, DOMAIN, DATA_COORDINATOR, CONF_MODE
from .coordinator import FourHeatDataUpdateCoordinator

_LOGGER = logging.getLogger(__name__)

CONFIG_SCHEMA = vol.Schema(
{
Expand Down Expand Up @@ -53,6 +56,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
hass,
config=entry.data,
options=entry.options,
id=entry.entry_id,
)

await coordinator.async_refresh()
Expand All @@ -64,6 +68,25 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
DATA_COORDINATOR: coordinator,
}


async def async_handle_set_value(call):
"""Handle the service call to set a value."""
entity_id = call.data.get('entity_id', '')
value = call.data.get('value', 5)

if valid_entity_id(entity_id):
e_id = hass.states.get(entity_id)
if e_id.attributes[ATTR_MARKER] == 'B':
c = hass.data[DOMAIN][e_id.attributes[ATTR_STOVE_ID]][DATA_COORDINATOR]
await c.async_set_value(e_id.attributes[ATTR_READING_ID], value)
else:
_LOGGER.error(f'"{entity_id}" is not valid to be set')
else:
_LOGGER.error(f'"{entity_id}" is no valid entity ID')


hass.services.async_register(DOMAIN, "set_value", async_handle_set_value)

hass.async_create_task(
hass.config_entries.async_forward_entry_setup(entry, "sensor")
)
Expand Down
19 changes: 18 additions & 1 deletion custom_components/4heat/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
class FourHeatDataUpdateCoordinator(DataUpdateCoordinator):
"""Class to manage fetching 4heat data."""

def __init__(self, hass: HomeAssistantType, *, config: dict, options: dict):
def __init__(self, hass: HomeAssistantType, *, config: dict, options: dict, id: str):
"""Initialize global 4heat data updater."""
self._host = config[CONF_HOST]
self._mode = False
self.swiches = [MODE_TYPE]
self.stove_id = id

if CONF_MODE in config:
self._mode = config[CONF_MODE]
Expand Down Expand Up @@ -129,3 +130,19 @@ async def async_unblock(self) -> bool:
except Exception as ex:
_LOGGER.error(ex)


async def async_set_value(self, id, value) -> bool:
val = str(value).zfill(12)
set_val = f'["SEC","1","B{id}{val}"]'.encode()
_LOGGER.debug(f"Command to send: {set_val}")
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(SOCKET_TIMEOUT)
s.connect((self._host, TCP_PORT))
s.send(set_val)
s.recv(SOCKET_BUFFER).decode()
s.close()
_LOGGER.debug("Set value")
except Exception as ex:
_LOGGER.error(ex)

18 changes: 12 additions & 6 deletions custom_components/4heat/services.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
boiler_on:
description: Start boiler
boiler_off:
description: Stop boiler
boiler_unblock:
description: Reset boiler error

set_value:
name: Set value
description: Sets val speed.
fields:
entity_id:
description: Id of the entiy to set
example: "sendor.something"
value:
description: value to set
example: "5"

0 comments on commit 5d31977

Please sign in to comment.