Demo: Remote improvements (#52265)

* Demo: Remote improvements

* Address pylint warning
This commit is contained in:
Franck Nijhof 2021-06-29 11:32:46 +02:00 committed by GitHub
parent a7dd7c1a3d
commit 8a82557142
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,14 +1,32 @@
"""Demo platform that has two fake remotes."""
from __future__ import annotations
from collections.abc import Iterable
from typing import Any
from homeassistant.components.remote import RemoteEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_DEFAULT_NAME
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType
async def async_setup_entry(hass, config_entry, async_add_entities):
async def async_setup_entry(
hass: HomeAssistant,
config_entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Demo config entry."""
setup_platform(hass, {}, async_add_entities)
def setup_platform(hass, config, add_entities_callback, discovery_info=None):
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities_callback: AddEntitiesCallback,
discovery_info: dict[str, Any] | None = None,
) -> None:
"""Set up the demo remotes."""
add_entities_callback(
[
@ -21,50 +39,32 @@ def setup_platform(hass, config, add_entities_callback, discovery_info=None):
class DemoRemote(RemoteEntity):
"""Representation of a demo remote."""
def __init__(self, name, state, icon):
_attr_should_poll = False
def __init__(self, name: str | None, state: bool, icon: str | None) -> None:
"""Initialize the Demo Remote."""
self._name = name or DEVICE_DEFAULT_NAME
self._state = state
self._icon = icon
self._attr_name = name or DEVICE_DEFAULT_NAME
self._attr_is_on = state
self._attr_icon = icon
self._last_command_sent = None
@property
def should_poll(self):
"""No polling needed for a demo remote."""
return False
@property
def name(self):
"""Return the name of the device if any."""
return self._name
@property
def icon(self):
"""Return the icon to use for device if any."""
return self._icon
@property
def is_on(self):
"""Return true if remote is on."""
return self._state
@property
def extra_state_attributes(self):
def extra_state_attributes(self) -> dict[str, Any] | None:
"""Return device state attributes."""
if self._last_command_sent is not None:
return {"last_command_sent": self._last_command_sent}
def turn_on(self, **kwargs):
def turn_on(self, **kwargs: Any) -> None:
"""Turn the remote on."""
self._state = True
self._attr_is_on = True
self.schedule_update_ha_state()
def turn_off(self, **kwargs):
def turn_off(self, **kwargs: Any) -> None:
"""Turn the remote off."""
self._state = False
self._attr_is_on = False
self.schedule_update_ha_state()
def send_command(self, command, **kwargs):
def send_command(self, command: Iterable[str], **kwargs: Any) -> None:
"""Send a command to a device."""
for com in command:
self._last_command_sent = com