* rebase * fixed review comments * fix test * Update tests * increase test coverage * implement some review comments * Enhance device check for old FWs and add tests * Reworked exception handling * small code optimizations * fix test * Increase test coverage * Update __init__.py changed from hass.config_entries.async_setup_platforms(entry, PLATFORMS) to await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) * Update __init__.py remove spaces * Bump python-mystrom to 2.2.0 * Migrate to get_device_info from python-mystrom * Migrate to get_device_info from python-mystrom * Update __init__.py * update requirements_all.txt * update config_flow * fix tests * Update homeassistant/components/mystrom/__init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Implemented review changes * increase test coverage * Implemented user defined title * implemented user defined title * Additional review comments * fix test * fix linter * fix linter * Update homeassistant/components/mystrom/models.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * fix review comments * fix missing import * simplify * Update homeassistant/components/mystrom/__init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/__init__.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_init.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_init.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_init.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_init.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/light.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update homeassistant/components/mystrom/switch.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * fix review comments * fix review comments * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * fix review comment * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> * Update tests/components/mystrom/test_config_flow.py Co-authored-by: epenet <6771947+epenet@users.noreply.github.com> --------- Co-authored-by: epenet <6771947+epenet@users.noreply.github.com>
103 lines
3.3 KiB
Python
103 lines
3.3 KiB
Python
"""Support for myStrom switches/plugs."""
|
|
from __future__ import annotations
|
|
|
|
import logging
|
|
from typing import Any
|
|
|
|
from pymystrom.exceptions import MyStromConnectionError
|
|
import voluptuous as vol
|
|
|
|
from homeassistant.components.switch import PLATFORM_SCHEMA, SwitchEntity
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
from homeassistant.const import CONF_HOST, CONF_NAME
|
|
from homeassistant.core import HomeAssistant
|
|
import homeassistant.helpers.config_validation as cv
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
|
|
|
from .const import DOMAIN, MANUFACTURER
|
|
|
|
DEFAULT_NAME = "myStrom Switch"
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
{
|
|
vol.Required(CONF_HOST): cv.string,
|
|
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
|
}
|
|
)
|
|
|
|
|
|
async def async_setup_entry(
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
) -> None:
|
|
"""Set up the myStrom entities."""
|
|
device = hass.data[DOMAIN][entry.entry_id].device
|
|
async_add_entities([MyStromSwitch(device, entry.title)])
|
|
|
|
|
|
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."""
|
|
async_create_issue(
|
|
hass,
|
|
DOMAIN,
|
|
"deprecated_yaml",
|
|
breaks_in_ha_version="2023.12.0",
|
|
is_fixable=False,
|
|
severity=IssueSeverity.WARNING,
|
|
translation_key="deprecated_yaml",
|
|
)
|
|
hass.async_create_task(
|
|
hass.config_entries.flow.async_init(
|
|
DOMAIN, context={"source": SOURCE_IMPORT}, data=config
|
|
)
|
|
)
|
|
|
|
|
|
class MyStromSwitch(SwitchEntity):
|
|
"""Representation of a myStrom switch/plug."""
|
|
|
|
def __init__(self, plug, name):
|
|
"""Initialize the myStrom switch/plug."""
|
|
self.plug = plug
|
|
self._attr_name = name
|
|
self._attr_unique_id = self.plug.mac
|
|
self._attr_device_info = DeviceInfo(
|
|
identifiers={(DOMAIN, self.plug.mac)},
|
|
name=name,
|
|
manufacturer=MANUFACTURER,
|
|
sw_version=self.plug.firmware,
|
|
)
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
"""Turn the switch on."""
|
|
try:
|
|
await self.plug.turn_on()
|
|
except MyStromConnectionError:
|
|
_LOGGER.error("No route to myStrom plug")
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
"""Turn the switch off."""
|
|
try:
|
|
await self.plug.turn_off()
|
|
except MyStromConnectionError:
|
|
_LOGGER.error("No route to myStrom plug")
|
|
|
|
async def async_update(self) -> None:
|
|
"""Get the latest data from the device and update the data."""
|
|
try:
|
|
await self.plug.get_state()
|
|
self._attr_is_on = self.plug.relay
|
|
self._attr_available = True
|
|
except MyStromConnectionError:
|
|
if self.available:
|
|
self._attr_available = False
|
|
_LOGGER.error("No route to myStrom plug")
|