Fix kira remote implementation (#77878)

pull/78586/head
epenet 2022-09-16 15:11:12 +02:00 committed by GitHub
parent 491177e5d3
commit f2a661026f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 51 deletions

View File

@ -1,13 +1,13 @@
"""Support for Keene Electronics IR-IP devices.""" """Support for Keene Electronics IR-IP devices."""
from __future__ import annotations from __future__ import annotations
import functools as ft from collections.abc import Iterable
import logging import logging
from typing import Any
from homeassistant.components import remote from homeassistant.components import remote
from homeassistant.const import CONF_DEVICE, CONF_NAME from homeassistant.const import CONF_DEVICE, CONF_NAME
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -31,32 +31,18 @@ def setup_platform(
add_entities([KiraRemote(device, kira)]) add_entities([KiraRemote(device, kira)])
class KiraRemote(Entity): class KiraRemote(remote.RemoteEntity):
"""Remote representation used to send commands to a Kira device.""" """Remote representation used to send commands to a Kira device."""
def __init__(self, name, kira): def __init__(self, name, kira):
"""Initialize KiraRemote class.""" """Initialize KiraRemote class."""
_LOGGER.debug("KiraRemote device init started for: %s", name) _LOGGER.debug("KiraRemote device init started for: %s", name)
self._name = name self._attr_name = name
self._kira = kira self._kira = kira
@property def send_command(self, command: Iterable[str], **kwargs: Any) -> None:
def name(self):
"""Return the Kira device's name."""
return self._name
def update(self) -> None:
"""No-op."""
def send_command(self, command, **kwargs):
"""Send a command to one device.""" """Send a command to one device."""
for single_command in command: for single_command in command:
code_tuple = (single_command, kwargs.get(remote.ATTR_DEVICE)) code_tuple = (single_command, kwargs.get(remote.ATTR_DEVICE))
_LOGGER.info("Sending Command: %s to %s", *code_tuple) _LOGGER.info("Sending Command: %s to %s", *code_tuple)
self._kira.sendCode(code_tuple) self._kira.sendCode(code_tuple)
async def async_send_command(self, command, **kwargs):
"""Send a command to a device."""
return await self.hass.async_add_executor_job(
ft.partial(self.send_command, command, **kwargs)
)

View File

@ -13,8 +13,6 @@ from . import CONF_SENSOR, DOMAIN
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ICON = "mdi:remote"
def setup_platform( def setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
@ -34,44 +32,20 @@ def setup_platform(
class KiraReceiver(SensorEntity): class KiraReceiver(SensorEntity):
"""Implementation of a Kira Receiver.""" """Implementation of a Kira Receiver."""
_attr_force_update = True # repeated states have meaning in Kira
_attr_icon = "mdi:remote"
_attr_should_poll = False _attr_should_poll = False
def __init__(self, name, kira): def __init__(self, name, kira):
"""Initialize the sensor.""" """Initialize the sensor."""
self._name = name self._attr_name = name
self._state = None self._attr_extra_state_attributes = {CONF_DEVICE: STATE_UNKNOWN}
self._device = STATE_UNKNOWN
kira.registerCallback(self._update_callback) kira.registerCallback(self._update_callback)
def _update_callback(self, code): def _update_callback(self, code):
code_name, device = code code_name, device = code
_LOGGER.debug("Kira Code: %s", code_name) _LOGGER.debug("Kira Code: %s", code_name)
self._state = code_name self._attr_native_value = code_name
self._device = device self._attr_extra_state_attributes[CONF_DEVICE] = device
self.schedule_update_ha_state() self.schedule_update_ha_state()
@property
def name(self):
"""Return the name of the receiver."""
return self._name
@property
def icon(self):
"""Return icon."""
return ICON
@property
def native_value(self):
"""Return the state of the receiver."""
return self._state
@property
def extra_state_attributes(self):
"""Return the state attributes of the device."""
return {CONF_DEVICE: self._device}
@property
def force_update(self) -> bool:
"""Kira should force updates. Repeated states have meaning."""
return True