core/homeassistant/components/goalzero/switch.py

62 lines
1.9 KiB
Python
Raw Normal View History

"""Support for Goal Zero Yeti Switches."""
from __future__ import annotations
2021-10-14 22:20:08 +00:00
from typing import Any, cast
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2022-06-05 01:50:38 +00:00
from .const import DOMAIN
from .entity import GoalZeroEntity
SWITCH_TYPES: tuple[SwitchEntityDescription, ...] = (
SwitchEntityDescription(
key="v12PortStatus",
name="12V port status",
),
SwitchEntityDescription(
key="usbPortStatus",
name="USB port status",
),
SwitchEntityDescription(
key="acPortStatus",
name="AC port status",
),
)
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up the Goal Zero Yeti switch."""
async_add_entities(
2022-06-05 01:50:38 +00:00
GoalZeroSwitch(
hass.data[DOMAIN][entry.entry_id],
description,
)
for description in SWITCH_TYPES
)
2022-06-05 01:50:38 +00:00
class GoalZeroSwitch(GoalZeroEntity, SwitchEntity):
"""Representation of a Goal Zero Yeti switch."""
@property
def is_on(self) -> bool:
"""Return state of the switch."""
2022-06-05 01:50:38 +00:00
return cast(bool, self._api.data[self.entity_description.key] == 1)
2021-10-14 22:20:08 +00:00
async def async_turn_off(self, **kwargs: Any) -> None:
"""Turn off the switch."""
payload = {self.entity_description.key: 0}
2022-06-05 01:50:38 +00:00
await self._api.post_state(payload=payload)
self.coordinator.async_set_updated_data(data=payload)
2021-10-14 22:20:08 +00:00
async def async_turn_on(self, **kwargs: Any) -> None:
"""Turn on the switch."""
payload = {self.entity_description.key: 1}
2022-06-05 01:50:38 +00:00
await self._api.post_state(payload=payload)
self.coordinator.async_set_updated_data(data=payload)