Skip to content

Commit

Permalink
Add buttons support
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Apr 5, 2024
1 parent f7ddc4a commit 1921db1
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 84 deletions.
2 changes: 1 addition & 1 deletion custom_components/yandex_station/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
_LOGGER = logging.getLogger(__name__)

PLATFORMS = [
"button",
"climate",
"light",
"humidifier",
"media_player",
"number",
"remote",
"select",
"switch",
"vacuum",
Expand Down
26 changes: 26 additions & 0 deletions custom_components/yandex_station/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from homeassistant.components.button import ButtonEntity

from .core.entity import YandexCustomEntity
from .hass import hass_utils

INCLUDE_CAPABILITIES = ("devices.capabilities.custom.button",)


async def async_setup_entry(hass, entry, async_add_entities):
entities = []

for quasar, device, config in hass_utils.incluce_devices(hass, entry):
if instances := config.get("capabilities"):
for instance in device["capabilities"]:
if instance["type"] not in INCLUDE_CAPABILITIES:
continue
if instance["parameters"].get("instance", "on") in instances:
entities.append(YandexCustomButton(quasar, device, instance))

async_add_entities(entities)


# noinspection PyAbstractClass
class YandexCustomButton(ButtonEntity, YandexCustomEntity):
async def async_press(self) -> None:
await self.quasar.device_action(self.device["id"], self.instance, True)
13 changes: 9 additions & 4 deletions custom_components/yandex_station/core/yandex_quasar.py
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,15 @@ async def get_device(self, deviceid: str):
return resp

async def device_action(self, deviceid: str, instance: str, value):
action = {
"type": IOT_TYPES[instance],
"state": {"instance": instance, "value": value},
}
action = {"state": {"instance": instance, "value": value}}

if instance in IOT_TYPES:
action["type"] = IOT_TYPES[instance]
elif instance.isdecimal():
action["type"] = "devices.capabilities.custom.button"
else:
return

r = await self.session.post(
f"{URL_USER}/devices/{deviceid}/actions", json={"actions": [action]}
)
Expand Down
3 changes: 2 additions & 1 deletion custom_components/yandex_station/hass/hass_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"devices.types.iron",
"devices.types.openable",
"devices.types.openable.curtain",
"devices.types.other",
"devices.types.pet_drinking_fountain",
"devices.types.pet_feeder",
"devices.types.washing_machine",
Expand Down Expand Up @@ -61,7 +62,7 @@ def incluce_devices(
for conf in includes:
for device in quasar.devices:
if isinstance(conf, str):
if conf == device["name"] or conf == device["id"]:
if conf == device["id"] or conf == device["name"]:
conf = build_include_config(device)
devices.append((quasar, device, conf))
break
Expand Down
78 changes: 0 additions & 78 deletions custom_components/yandex_station/remote.py

This file was deleted.

0 comments on commit 1921db1

Please sign in to comment.