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."""
from __future__ import annotations
import functools as ft
from collections.abc import Iterable
import logging
from typing import Any
from homeassistant.components import remote
from homeassistant.const import CONF_DEVICE, CONF_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import Entity
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
@ -31,32 +31,18 @@ def setup_platform(
add_entities([KiraRemote(device, kira)])
class KiraRemote(Entity):
class KiraRemote(remote.RemoteEntity):
"""Remote representation used to send commands to a Kira device."""
def __init__(self, name, kira):
"""Initialize KiraRemote class."""
_LOGGER.debug("KiraRemote device init started for: %s", name)
self._name = name
self._attr_name = name
self._kira = kira
@property
def name(self):
"""Return the Kira device's name."""
return self._name
def update(self) -> None:
"""No-op."""
def send_command(self, command, **kwargs):
def send_command(self, command: Iterable[str], **kwargs: Any) -> None:
"""Send a command to one device."""
for single_command in command:
code_tuple = (single_command, kwargs.get(remote.ATTR_DEVICE))
_LOGGER.info("Sending Command: %s to %s", *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__)
ICON = "mdi:remote"
def setup_platform(
hass: HomeAssistant,
@ -34,44 +32,20 @@ def setup_platform(
class KiraReceiver(SensorEntity):
"""Implementation of a Kira Receiver."""
_attr_force_update = True # repeated states have meaning in Kira
_attr_icon = "mdi:remote"
_attr_should_poll = False
def __init__(self, name, kira):
"""Initialize the sensor."""
self._name = name
self._state = None
self._device = STATE_UNKNOWN
self._attr_name = name
self._attr_extra_state_attributes = {CONF_DEVICE: STATE_UNKNOWN}
kira.registerCallback(self._update_callback)
def _update_callback(self, code):
code_name, device = code
_LOGGER.debug("Kira Code: %s", code_name)
self._state = code_name
self._device = device
self._attr_native_value = code_name
self._attr_extra_state_attributes[CONF_DEVICE] = device
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