Home Assistant Unofficial Reference 2024.12.1
__init__.py
Go to the documentation of this file.
1 """The myStrom integration."""
2 
3 from __future__ import annotations
4 
5 import logging
6 
7 import pymystrom
8 from pymystrom.bulb import MyStromBulb
9 from pymystrom.exceptions import MyStromConnectionError
10 from pymystrom.switch import MyStromSwitch
11 
12 from homeassistant.config_entries import ConfigEntry
13 from homeassistant.const import CONF_HOST, Platform
14 from homeassistant.core import HomeAssistant
15 from homeassistant.exceptions import ConfigEntryNotReady
16 
17 from .const import DOMAIN
18 from .models import MyStromData
19 
20 PLATFORMS_PLUGS = [Platform.SENSOR, Platform.SWITCH]
21 PLATFORMS_BULB = [Platform.LIGHT]
22 
23 _LOGGER = logging.getLogger(__name__)
24 
25 
27  device: MyStromSwitch | MyStromBulb, ip_address: str
28 ) -> None:
29  try:
30  await device.get_state()
31  except MyStromConnectionError as err:
32  _LOGGER.error("No route to myStrom plug: %s", ip_address)
33  raise ConfigEntryNotReady from err
34 
35 
36 def _get_mystrom_bulb(host: str, mac: str) -> MyStromBulb:
37  return MyStromBulb(host, mac)
38 
39 
40 def _get_mystrom_switch(host: str) -> MyStromSwitch:
41  return MyStromSwitch(host)
42 
43 
44 async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
45  """Set up myStrom from a config entry."""
46  host = entry.data[CONF_HOST]
47  try:
48  info = await pymystrom.get_device_info(host)
49  except MyStromConnectionError as err:
50  _LOGGER.error("No route to myStrom plug: %s", host)
51  raise ConfigEntryNotReady from err
52 
53  info.setdefault("type", 101)
54 
55  device_type = info["type"]
56  if device_type in [101, 106, 107, 120]:
57  device = _get_mystrom_switch(host)
58  platforms = PLATFORMS_PLUGS
59  await _async_get_device_state(device, info["ip"])
60  elif device_type in [102, 105]:
61  mac = info["mac"]
62  device = _get_mystrom_bulb(host, mac)
63  platforms = PLATFORMS_BULB
64  await _async_get_device_state(device, info["ip"])
65  if device.bulb_type not in ["rgblamp", "strip"]:
66  _LOGGER.error(
67  "Device %s (%s) is not a myStrom bulb nor myStrom LED Strip",
68  host,
69  mac,
70  )
71  return False
72  else:
73  _LOGGER.error("Unsupported myStrom device type: %s", device_type)
74  return False
75 
76  hass.data.setdefault(DOMAIN, {})[entry.entry_id] = MyStromData(
77  device=device,
78  info=info,
79  )
80  await hass.config_entries.async_forward_entry_setups(entry, platforms)
81 
82  return True
83 
84 
85 async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
86  """Unload a config entry."""
87  device_type = hass.data[DOMAIN][entry.entry_id].info["type"]
88  platforms = []
89  if device_type in [101, 106, 107, 120]:
90  platforms.extend(PLATFORMS_PLUGS)
91  elif device_type in [102, 105]:
92  platforms.extend(PLATFORMS_BULB)
93  if unload_ok := await hass.config_entries.async_unload_platforms(entry, platforms):
94  hass.data[DOMAIN].pop(entry.entry_id)
95 
96  return unload_ok
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:85
None _async_get_device_state(MyStromSwitch|MyStromBulb device, str ip_address)
Definition: __init__.py:28
MyStromSwitch _get_mystrom_switch(str host)
Definition: __init__.py:40
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
Definition: __init__.py:44
MyStromBulb _get_mystrom_bulb(str host, str mac)
Definition: __init__.py:36