mirror of
https://github.com/home-assistant/core.git
synced 2025-04-26 10:17:51 +00:00

* Initial commit with errors * Commitable * Use triggercmd user id as hub name * Validate the token * Use switch type, no trigger yet * Working integration * Use triggercmd module instead of httpx * Add tests for triggercmd integration * Add triggercmd to requirements_test_all.txt * Add untested triggercmd files to .coveragerc * Implement cgarwood's PR suggestions * Address PR feedback * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/triggercmd/hub.py Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/triggercmd/strings.json Co-authored-by: Robert Resch <robert@resch.dev> * Update homeassistant/components/triggercmd/hub.py Co-authored-by: Robert Resch <robert@resch.dev> * Get user id via triggercmd module, and better check for status 200 code * PR feedback fixes * Update homeassistant/components/triggercmd/switch.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/switch.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * More PR feedback fixes * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/strings.json Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/switch.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * More PR feedback fixes * Update tests/components/triggercmd/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Changes for PR feedback * Changes to address PR comments * Fix connection error when no internet * Update homeassistant/components/triggercmd/__init__.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update homeassistant/components/triggercmd/config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Update tests/components/triggercmd/test_config_flow.py Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com> * Updates for PR feedback * Update tests/components/triggercmd/test_config_flow.py --------- Co-authored-by: Robert Resch <robert@resch.dev> Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
86 lines
2.5 KiB
Python
86 lines
2.5 KiB
Python
"""Platform for switch integration."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
|
|
from triggercmd import client, ha
|
|
|
|
from homeassistant.components.switch import SwitchEntity
|
|
from homeassistant.core import HomeAssistant
|
|
from homeassistant.helpers.device_registry import DeviceInfo
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
from . import TriggercmdConfigEntry
|
|
from .const import DOMAIN
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant,
|
|
config_entry: TriggercmdConfigEntry,
|
|
async_add_entities: AddEntitiesCallback,
|
|
) -> None:
|
|
"""Add switch for passed config_entry in HA."""
|
|
hub = config_entry.runtime_data
|
|
async_add_entities(TRIGGERcmdSwitch(trigger) for trigger in hub.triggers)
|
|
|
|
|
|
class TRIGGERcmdSwitch(SwitchEntity):
|
|
"""Representation of a Switch."""
|
|
|
|
_attr_has_entity_name = True
|
|
_attr_assumed_state = True
|
|
_attr_should_poll = False
|
|
|
|
computer_id: str
|
|
trigger_id: str
|
|
firmware_version: str
|
|
model: str
|
|
hub: ha.Hub
|
|
|
|
def __init__(self, trigger: TRIGGERcmdSwitch) -> None:
|
|
"""Initialize the switch."""
|
|
self._switch = trigger
|
|
self._attr_is_on = False
|
|
self._attr_unique_id = f"{trigger.computer_id}.{trigger.trigger_id}"
|
|
self._attr_name = trigger.trigger_id
|
|
self._attr_device_info = DeviceInfo(
|
|
identifiers={(DOMAIN, trigger.computer_id)},
|
|
name=trigger.computer_id.capitalize(),
|
|
sw_version=trigger.firmware_version,
|
|
model=trigger.model,
|
|
manufacturer=trigger.hub.manufacturer,
|
|
)
|
|
|
|
@property
|
|
def available(self) -> bool:
|
|
"""Return True if hub is available."""
|
|
return self._switch.hub.online
|
|
|
|
async def async_turn_on(self, **kwargs):
|
|
"""Turn the switch on."""
|
|
await self.trigger("on")
|
|
self._attr_is_on = True
|
|
self.async_write_ha_state()
|
|
|
|
async def async_turn_off(self, **kwargs):
|
|
"""Turn the switch off."""
|
|
await self.trigger("off")
|
|
self._attr_is_on = False
|
|
self.async_write_ha_state()
|
|
|
|
async def trigger(self, params: str):
|
|
"""Trigger the command."""
|
|
r = await client.async_trigger(
|
|
self._switch.hub.token,
|
|
{
|
|
"computer": self._switch.computer_id,
|
|
"trigger": self._switch.trigger_id,
|
|
"params": params,
|
|
"sender": "Home Assistant",
|
|
},
|
|
)
|
|
_LOGGER.debug("TRIGGERcmd trigger response: %s", r.json())
|