core/homeassistant/components/mystrom/switch.py

104 lines
3.0 KiB
Python
Raw Normal View History

"""Support for myStrom switches/plugs."""
from __future__ import annotations
2015-11-25 16:21:11 +00:00
import logging
2016-02-19 05:27:50 +00:00
from pymystrom.exceptions import MyStromConnectionError
from pymystrom.switch import MyStromSwitch as _MyStromSwitch
import voluptuous as vol
2015-11-25 16:21:11 +00:00
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
from homeassistant.const import CONF_HOST, CONF_NAME
from homeassistant.core import HomeAssistant
2020-02-01 22:04:42 +00:00
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2015-11-25 16:21:11 +00:00
2019-07-31 19:25:30 +00:00
DEFAULT_NAME = "myStrom Switch"
2015-11-25 16:21:11 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
}
)
2015-11-25 16:21:11 +00:00
async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the myStrom switch/plug integration."""
name = config.get(CONF_NAME)
host = config.get(CONF_HOST)
2015-11-25 16:21:11 +00:00
try:
plug = _MyStromSwitch(host)
await plug.get_state()
except MyStromConnectionError as err:
_LOGGER.error("No route to myStrom plug: %s", host)
raise PlatformNotReady() from err
2015-11-25 16:21:11 +00:00
async_add_entities([MyStromSwitch(plug, name)])
2015-11-25 16:21:11 +00:00
class MyStromSwitch(SwitchEntity):
"""Representation of a myStrom switch/plug."""
2016-03-08 12:35:39 +00:00
def __init__(self, plug, name):
"""Initialize the myStrom switch/plug."""
2015-11-25 16:21:11 +00:00
self._name = name
self.plug = plug
2020-02-01 22:04:42 +00:00
self._available = True
self.relay = None
2015-11-25 16:21:11 +00:00
@property
def name(self):
2016-03-08 12:35:39 +00:00
"""Return the name of the switch."""
2015-11-25 16:21:11 +00:00
return self._name
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if switch is on."""
return bool(self.relay)
@property
def unique_id(self):
"""Return a unique ID."""
return self.plug._mac # pylint: disable=protected-access
2015-11-25 16:21:11 +00:00
2020-02-01 22:04:42 +00:00
@property
def available(self):
"""Could the device be accessed during the last update call."""
return self._available
async def async_turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the switch on."""
2015-11-25 17:07:29 +00:00
try:
await self.plug.turn_on()
except MyStromConnectionError:
_LOGGER.error("No route to myStrom plug")
2015-11-25 16:21:11 +00:00
async def async_turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn the switch off."""
2015-11-25 17:07:29 +00:00
try:
await self.plug.turn_off()
except MyStromConnectionError:
_LOGGER.error("No route to myStrom plug")
2015-11-25 16:21:11 +00:00
async def async_update(self):
"""Get the latest data from the device and update the data."""
2015-11-25 16:21:11 +00:00
try:
await self.plug.get_state()
self.relay = self.plug.relay
2020-02-01 22:04:42 +00:00
self._available = True
except MyStromConnectionError:
if self._available:
self._available = False
_LOGGER.error("No route to myStrom plug")