1 """Connect to a MySensors gateway via pymysensors API."""
3 from __future__
import annotations
5 from collections.abc
import Callable, Mapping
8 from mysensors
import BaseAsyncGateway
18 MYSENSORS_DISCOVERED_NODES,
26 from .entity
import MySensorsChildEntity, get_mysensors_devices
27 from .gateway
import finish_setup, gw_stop, setup_gateway
29 _LOGGER = logging.getLogger(__name__)
31 DATA_HASS_CONFIG =
"hass_config"
35 """Set up an instance of the MySensors integration.
37 Every instance has a connection to exactly one Gateway.
42 _LOGGER.error(
"Gateway setup failed for %s", entry.data)
45 mysensors_data = hass.data.setdefault(DOMAIN, {})
46 if MYSENSORS_GATEWAYS
not in mysensors_data:
47 mysensors_data[MYSENSORS_GATEWAYS] = {}
48 mysensors_data[MYSENSORS_GATEWAYS][entry.entry_id] = gateway
50 await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)
57 """Remove an instance of the MySensors integration."""
59 gateway: BaseAsyncGateway = hass.data[DOMAIN][MYSENSORS_GATEWAYS][entry.entry_id]
61 unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
65 key = MYSENSORS_ON_UNLOAD.format(entry.entry_id)
66 if key
in hass.data[DOMAIN]:
67 for fnct
in hass.data[DOMAIN][key]:
70 hass.data[DOMAIN].pop(key)
72 del hass.data[DOMAIN][MYSENSORS_GATEWAYS][entry.entry_id]
73 hass.data[DOMAIN].pop(MYSENSORS_DISCOVERED_NODES.format(entry.entry_id),
None)
75 await
gw_stop(hass, entry, gateway)
80 hass: HomeAssistant, config_entry: ConfigEntry, device_entry: DeviceEntry
82 """Remove a MySensors config entry from a device."""
83 gateway: BaseAsyncGateway = hass.data[DOMAIN][MYSENSORS_GATEWAYS][
87 device_id
for domain, device_id
in device_entry.identifiers
if domain == DOMAIN
89 node_id =
int(device_id.partition(
"-")[2])
90 gateway.sensors.pop(node_id,
None)
91 gateway.tasks.persistence.need_save =
True
94 hass.data[DOMAIN].setdefault(
95 MYSENSORS_DISCOVERED_NODES.format(config_entry.entry_id), set()
105 discovery_info: DiscoveryInfo,
106 device_class: type[MySensorsChildEntity]
107 | Mapping[SensorType, type[MySensorsChildEntity]],
111 async_add_entities: Callable |
None =
None,
112 ) -> list[MySensorsChildEntity] |
None:
113 """Set up a MySensors platform.
115 Sets up a bunch of instances of a single platform that is supported by this
118 The function is given a list of device ids, each one describing an instance
119 to set up. The function is also given a class.
121 A new instance of the class is created for every device id, and the device
122 id is given to the constructor of the class.
124 if device_args
is None:
126 new_devices: list[MySensorsChildEntity] = []
127 new_dev_ids: list[DevId] = discovery_info[ATTR_DEVICES]
128 for dev_id
in new_dev_ids:
130 if dev_id
in devices:
132 "Skipping setup of %s for platform %s as it already exists",
137 gateway_id, node_id, child_id, value_type = dev_id
138 gateway: BaseAsyncGateway = hass.data[DOMAIN][MYSENSORS_GATEWAYS][gateway_id]
140 if isinstance(device_class, dict):
141 child = gateway.sensors[node_id].children[child_id]
142 s_type = gateway.const.Presentation(child.type).name
143 device_class_copy = device_class[s_type]
145 device_class_copy = device_class
147 args_copy = (*device_args, gateway_id, gateway, node_id, child_id, value_type)
148 devices[dev_id] = device_class_copy(*args_copy)
149 new_devices.append(devices[dev_id])
151 _LOGGER.debug(
"Adding new devices: %s", new_devices)
152 if async_add_entities
is not None:
bool remove(self, _T matcher)
None finish_setup(HomeAssistant hass, ConfigType config)
dict[DevId, MySensorsChildEntity] get_mysensors_devices(HomeAssistant hass, Platform domain)
BaseAsyncGateway|None setup_gateway(HomeAssistant hass, ConfigEntry entry)
None gw_stop(HomeAssistant hass, ConfigEntry entry, BaseAsyncGateway gateway)
bool async_setup_entry(HomeAssistant hass, ConfigEntry entry)
list[MySensorsChildEntity]|None setup_mysensors_platform(HomeAssistant hass, Platform domain, DiscoveryInfo discovery_info, type[MySensorsChildEntity]|Mapping[SensorType, type[MySensorsChildEntity]] device_class,(tuple|None) device_args=None, Callable|None async_add_entities=None)
bool async_remove_config_entry_device(HomeAssistant hass, ConfigEntry config_entry, DeviceEntry device_entry)
bool async_unload_entry(HomeAssistant hass, ConfigEntry entry)