Address late review comments on homeworks PRs (#114867)

pull/114890/head^2
Erik Montnemery 2024-04-04 22:48:31 +02:00 committed by GitHub
parent 95ef087fa8
commit b0d1b6555d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 40 additions and 35 deletions

View File

@ -2,10 +2,10 @@
from __future__ import annotations
import asyncio
from collections.abc import Mapping
from dataclasses import dataclass
import logging
from time import sleep
from typing import Any
from pyhomeworks.pyhomeworks import HW_BUTTON_PRESSED, HW_BUTTON_RELEASED, Homeworks
@ -129,19 +129,6 @@ async def async_send_command(hass: HomeAssistant, data: Mapping[str, Any]) -> No
return data
return None
def send_commands(controller: Homeworks, commands: list[str]) -> None:
"""Send commands to controller."""
_LOGGER.debug("Send commands: %s", commands)
for command in commands:
if command.lower().startswith("delay"):
delay = int(command.partition(" ")[2])
_LOGGER.debug("Sleeping for %s ms", delay)
sleep(delay / 1000)
else:
_LOGGER.debug("Sending command '%s'", command)
# pylint: disable-next=protected-access
controller._send(command)
homeworks_data = get_homeworks_data(data[CONF_CONTROLLER_ID])
if not homeworks_data:
raise ServiceValidationError(
@ -153,9 +140,20 @@ async def async_send_command(hass: HomeAssistant, data: Mapping[str, Any]) -> No
},
)
await hass.async_add_executor_job(
send_commands, homeworks_data.controller, data[CONF_COMMAND]
)
commands = data[CONF_COMMAND]
_LOGGER.debug("Send commands: %s", commands)
for command in commands:
if command.lower().startswith("delay"):
delay = int(command.partition(" ")[2])
_LOGGER.debug("Sleeping for %s ms", delay)
await asyncio.sleep(delay / 1000)
else:
_LOGGER.debug("Sending command '%s'", command)
await hass.async_add_executor_job(
# pylint: disable-next=protected-access
homeworks_data.controller._send,
command,
)
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:

View File

@ -36,12 +36,12 @@ async def async_setup_entry(
data: HomeworksData = hass.data[DOMAIN][entry.entry_id]
controller = data.controller
controller_id = entry.options[CONF_CONTROLLER_ID]
devs = []
entities = []
for keypad in entry.options.get(CONF_KEYPADS, []):
for button in keypad[CONF_BUTTONS]:
if not button[CONF_LED]:
continue
dev = HomeworksBinarySensor(
entity = HomeworksBinarySensor(
controller,
data.keypads[keypad[CONF_ADDR]],
controller_id,
@ -50,8 +50,8 @@ async def async_setup_entry(
button[CONF_NAME],
button[CONF_NUMBER],
)
devs.append(dev)
async_add_entities(devs, True)
entities.append(entity)
async_add_entities(entities, True)
class HomeworksBinarySensor(HomeworksEntity, BinarySensorEntity):

View File

@ -2,7 +2,7 @@
from __future__ import annotations
from time import sleep
import asyncio
from pyhomeworks.pyhomeworks import Homeworks
@ -32,10 +32,10 @@ async def async_setup_entry(
data: HomeworksData = hass.data[DOMAIN][entry.entry_id]
controller = data.controller
controller_id = entry.options[CONF_CONTROLLER_ID]
devs = []
entities = []
for keypad in entry.options.get(CONF_KEYPADS, []):
for button in keypad[CONF_BUTTONS]:
dev = HomeworksButton(
entity = HomeworksButton(
controller,
controller_id,
keypad[CONF_ADDR],
@ -44,8 +44,8 @@ async def async_setup_entry(
button[CONF_NUMBER],
button[CONF_RELEASE_DELAY],
)
devs.append(dev)
async_add_entities(devs, True)
entities.append(entity)
async_add_entities(entities, True)
class HomeworksButton(HomeworksEntity, ButtonEntity):
@ -68,12 +68,19 @@ class HomeworksButton(HomeworksEntity, ButtonEntity):
)
self._release_delay = release_delay
def press(self) -> None:
async def async_press(self) -> None:
"""Press the button."""
# pylint: disable-next=protected-access
self._controller._send(f"KBP, {self._addr}, {self._idx}")
await self.hass.async_add_executor_job(
# pylint: disable-next=protected-access
self._controller._send,
f"KBP, {self._addr}, {self._idx}",
)
if not self._release_delay:
return
sleep(self._release_delay)
await asyncio.sleep(self._release_delay)
# pylint: disable-next=protected-access
self._controller._send(f"KBR, {self._addr}, {self._idx}")
await self.hass.async_add_executor_job(
# pylint: disable-next=protected-access
self._controller._send,
f"KBR, {self._addr}, {self._idx}",
)

View File

@ -28,17 +28,17 @@ async def async_setup_entry(
data: HomeworksData = hass.data[DOMAIN][entry.entry_id]
controller = data.controller
controller_id = entry.options[CONF_CONTROLLER_ID]
devs = []
entities = []
for dimmer in entry.options.get(CONF_DIMMERS, []):
dev = HomeworksLight(
entity = HomeworksLight(
controller,
controller_id,
dimmer[CONF_ADDR],
dimmer[CONF_NAME],
dimmer[CONF_RATE],
)
devs.append(dev)
async_add_entities(devs, True)
entities.append(entity)
async_add_entities(entities, True)
class HomeworksLight(HomeworksEntity, LightEntity):