core/homeassistant/components/madvr/remote.py

77 lines
2.3 KiB
Python
Raw Normal View History

Add madvr envy integration (#120382) * feat: Add madvr envy * fix: await and pass entry directly * fix: add attributes and unique id for sensors * fix: reflect power state well, improve state detection * fix: don't connect on init, add options, add reload on change, keep on during test * fix: cancel tasks on unload * fix: test connection via library * fix: wait for boot time * docs: add readme and license * fix: broken pipe in lib * fix: detect out of band power off * fix: improve extra attributes * fix: fix unloading, add config flow test, limit to one platform * fix: use conf, refresh coordinator, other comments * fix: remove event data * fix: fix tests passing, remove wake on lan * fix: dont allow to proceed unless connection works * chore: update dep * fix: update config flow, add constants * fix: write state, use runtime data instead * fix: remove await * fix: move unloading and stuff to coordinator/init * fix: pass in config entry with correct type * fix: move queue and tasks to library * fix: config flow error flow, tests, name, and update lib * fix: update lib, leave connection open on setup * fix: update lib * fix: address comments, remove wol from lib * fix: remove unneeded options * fix: remove fields * fix: simplify code, address comments * fix: move error to lib * fix: fix test * fix: stronger types * fix: update lib * fix: missing text from options flow * chore: remove options flow * chore: remove import * chore: update comments * fix: get mac from device, persist * fix: add mac stuff to test * fix: startup import errors * chore: stale comment * fix: get mac from persisted config * chore: update lib * fix: persist mac in a better way * feat: use mac as unique ID for entry * fix: use unique ID from mac, add proper device * fix: will not be set in init potentially * fix: access mac * fix: optimize, move error to lib * feat: add coordinator test, use conf * fix: use one mock, add init test * fix: not async * feat: add remote test * fix: types * fix: patch client, expand remote tests * fix: use snapshot test * fix: update branding * fix: add description, fix type check * fix: update tests * fix: test * fix: update test * fix: camelcase * Fix * feat: strict typing * fix: strict typing in lib * fix: type will never be None * fix: reference to mac, all tests passing --------- Co-authored-by: Joostlek <joostlek@outlook.com>
2024-07-07 18:41:53 +00:00
"""Support for madVR remote control."""
from __future__ import annotations
from collections.abc import Iterable
import logging
from typing import Any
from homeassistant.components.remote import RemoteEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import MadVRConfigEntry
from .coordinator import MadVRCoordinator
from .entity import MadVREntity
Add madvr envy integration (#120382) * feat: Add madvr envy * fix: await and pass entry directly * fix: add attributes and unique id for sensors * fix: reflect power state well, improve state detection * fix: don't connect on init, add options, add reload on change, keep on during test * fix: cancel tasks on unload * fix: test connection via library * fix: wait for boot time * docs: add readme and license * fix: broken pipe in lib * fix: detect out of band power off * fix: improve extra attributes * fix: fix unloading, add config flow test, limit to one platform * fix: use conf, refresh coordinator, other comments * fix: remove event data * fix: fix tests passing, remove wake on lan * fix: dont allow to proceed unless connection works * chore: update dep * fix: update config flow, add constants * fix: write state, use runtime data instead * fix: remove await * fix: move unloading and stuff to coordinator/init * fix: pass in config entry with correct type * fix: move queue and tasks to library * fix: config flow error flow, tests, name, and update lib * fix: update lib, leave connection open on setup * fix: update lib * fix: address comments, remove wol from lib * fix: remove unneeded options * fix: remove fields * fix: simplify code, address comments * fix: move error to lib * fix: fix test * fix: stronger types * fix: update lib * fix: missing text from options flow * chore: remove options flow * chore: remove import * chore: update comments * fix: get mac from device, persist * fix: add mac stuff to test * fix: startup import errors * chore: stale comment * fix: get mac from persisted config * chore: update lib * fix: persist mac in a better way * feat: use mac as unique ID for entry * fix: use unique ID from mac, add proper device * fix: will not be set in init potentially * fix: access mac * fix: optimize, move error to lib * feat: add coordinator test, use conf * fix: use one mock, add init test * fix: not async * feat: add remote test * fix: types * fix: patch client, expand remote tests * fix: use snapshot test * fix: update branding * fix: add description, fix type check * fix: update tests * fix: test * fix: update test * fix: camelcase * Fix * feat: strict typing * fix: strict typing in lib * fix: type will never be None * fix: reference to mac, all tests passing --------- Co-authored-by: Joostlek <joostlek@outlook.com>
2024-07-07 18:41:53 +00:00
_LOGGER = logging.getLogger(__name__)
async def async_setup_entry(
hass: HomeAssistant,
entry: MadVRConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the madVR remote."""
coordinator = entry.runtime_data
async_add_entities(
[
MadvrRemote(coordinator),
]
)
class MadvrRemote(MadVREntity, RemoteEntity):
Add madvr envy integration (#120382) * feat: Add madvr envy * fix: await and pass entry directly * fix: add attributes and unique id for sensors * fix: reflect power state well, improve state detection * fix: don't connect on init, add options, add reload on change, keep on during test * fix: cancel tasks on unload * fix: test connection via library * fix: wait for boot time * docs: add readme and license * fix: broken pipe in lib * fix: detect out of band power off * fix: improve extra attributes * fix: fix unloading, add config flow test, limit to one platform * fix: use conf, refresh coordinator, other comments * fix: remove event data * fix: fix tests passing, remove wake on lan * fix: dont allow to proceed unless connection works * chore: update dep * fix: update config flow, add constants * fix: write state, use runtime data instead * fix: remove await * fix: move unloading and stuff to coordinator/init * fix: pass in config entry with correct type * fix: move queue and tasks to library * fix: config flow error flow, tests, name, and update lib * fix: update lib, leave connection open on setup * fix: update lib * fix: address comments, remove wol from lib * fix: remove unneeded options * fix: remove fields * fix: simplify code, address comments * fix: move error to lib * fix: fix test * fix: stronger types * fix: update lib * fix: missing text from options flow * chore: remove options flow * chore: remove import * chore: update comments * fix: get mac from device, persist * fix: add mac stuff to test * fix: startup import errors * chore: stale comment * fix: get mac from persisted config * chore: update lib * fix: persist mac in a better way * feat: use mac as unique ID for entry * fix: use unique ID from mac, add proper device * fix: will not be set in init potentially * fix: access mac * fix: optimize, move error to lib * feat: add coordinator test, use conf * fix: use one mock, add init test * fix: not async * feat: add remote test * fix: types * fix: patch client, expand remote tests * fix: use snapshot test * fix: update branding * fix: add description, fix type check * fix: update tests * fix: test * fix: update test * fix: camelcase * Fix * feat: strict typing * fix: strict typing in lib * fix: type will never be None * fix: reference to mac, all tests passing --------- Co-authored-by: Joostlek <joostlek@outlook.com>
2024-07-07 18:41:53 +00:00
"""Remote entity for the madVR integration."""
_attr_name = None
def __init__(
self,
coordinator: MadVRCoordinator,
) -> None:
"""Initialize the remote entity."""
super().__init__(coordinator)
self.madvr_client = coordinator.client
self._attr_unique_id = coordinator.mac
@property
def is_on(self) -> bool:
"""Return true if the device is on."""
return self.madvr_client.is_on
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the device."""
_LOGGER.debug("Turning off")
try:
await self.madvr_client.power_off()
except (ConnectionError, NotImplementedError) as err:
_LOGGER.error("Failed to turn off device %s", err)
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the device."""
_LOGGER.debug("Turning on device")
try:
await self.madvr_client.power_on(mac=self.coordinator.mac)
except (ConnectionError, NotImplementedError) as err:
_LOGGER.error("Failed to turn on device %s", err)
async def async_send_command(self, command: Iterable[str], **kwargs: Any) -> None:
"""Send a command to one device."""
_LOGGER.debug("adding command %s", command)
try:
await self.madvr_client.add_command_to_queue(command)
except (ConnectionError, NotImplementedError) as err:
_LOGGER.error("Failed to send command %s", err)