ha-core/homeassistant/components/mysensors/handler.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
3.7 KiB
Python
Raw Normal View History

"""Handle MySensors messages."""
2021-03-18 13:07:04 +01:00
from __future__ import annotations
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
from collections.abc import Callable
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
from mysensors import Message
from mysensors.const import SYSTEM_CHILD_ID
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
2022-01-08 20:55:24 +01:00
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.dispatcher import async_dispatcher_send
from homeassistant.util import decorator
from .const import CHILD_CALLBACK, NODE_CALLBACK, DevId, GatewayId
from .device import get_mysensors_devices
from .helpers import (
discover_mysensors_node,
discover_mysensors_platform,
validate_set_msg,
)
HANDLERS: decorator.Registry[
str, Callable[[HomeAssistant, GatewayId, Message], None]
] = decorator.Registry()
@HANDLERS.register("set")
@callback
def handle_set(hass: HomeAssistant, gateway_id: GatewayId, msg: Message) -> None:
"""Handle a mysensors set message."""
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
validated = validate_set_msg(gateway_id, msg)
_handle_child_update(hass, gateway_id, validated)
@HANDLERS.register("internal")
@callback
def handle_internal(hass: HomeAssistant, gateway_id: GatewayId, msg: Message) -> None:
"""Handle a mysensors internal message."""
internal = msg.gateway.const.Internal(msg.sub_type)
2021-10-18 15:54:38 +02:00
if (handler := HANDLERS.get(internal.name)) is None:
return
handler(hass, gateway_id, msg)
@HANDLERS.register("I_BATTERY_LEVEL")
@callback
def handle_battery_level(
hass: HomeAssistant, gateway_id: GatewayId, msg: Message
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
) -> None:
"""Handle an internal battery level message."""
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
_handle_node_update(hass, gateway_id, msg)
@HANDLERS.register("I_HEARTBEAT_RESPONSE")
@callback
def handle_heartbeat(hass: HomeAssistant, gateway_id: GatewayId, msg: Message) -> None:
"""Handle an heartbeat."""
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
_handle_node_update(hass, gateway_id, msg)
@HANDLERS.register("I_SKETCH_NAME")
@callback
def handle_sketch_name(
hass: HomeAssistant, gateway_id: GatewayId, msg: Message
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
) -> None:
"""Handle an internal sketch name message."""
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
_handle_node_update(hass, gateway_id, msg)
@HANDLERS.register("I_SKETCH_VERSION")
@callback
def handle_sketch_version(
hass: HomeAssistant, gateway_id: GatewayId, msg: Message
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
) -> None:
"""Handle an internal sketch version message."""
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
_handle_node_update(hass, gateway_id, msg)
@HANDLERS.register("presentation")
@callback
def handle_presentation(
hass: HomeAssistant, gateway_id: GatewayId, msg: Message
) -> None:
"""Handle an internal presentation message."""
if msg.child_id == SYSTEM_CHILD_ID:
discover_mysensors_node(hass, gateway_id, msg.node_id)
@callback
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
def _handle_child_update(
2022-01-08 20:55:24 +01:00
hass: HomeAssistant, gateway_id: GatewayId, validated: dict[Platform, list[DevId]]
2021-06-07 16:04:04 +02:00
) -> None:
"""Handle a child update."""
2021-03-18 13:07:04 +01:00
signals: list[str] = []
# Update all platforms for the device via dispatcher.
# Add/update entity for validated children.
for platform, dev_ids in validated.items():
devices = get_mysensors_devices(hass, platform)
2021-03-18 13:07:04 +01:00
new_dev_ids: list[DevId] = []
for dev_id in dev_ids:
if dev_id in devices:
signals.append(CHILD_CALLBACK.format(*dev_id))
else:
new_dev_ids.append(dev_id)
if new_dev_ids:
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
discover_mysensors_platform(hass, gateway_id, platform, new_dev_ids)
for signal in set(signals):
# Only one signal per device is needed.
# A device can have multiple platforms, ie multiple schemas.
async_dispatcher_send(hass, signal)
@callback
2021-06-07 16:04:04 +02:00
def _handle_node_update(
hass: HomeAssistant, gateway_id: GatewayId, msg: Message
) -> None:
"""Handle a node update."""
MySensors config flow (#45421) * MySensors: Add type annotations Adds a bunch of type annotations that were created while understanding the code. * MySensors: Change GatewayId to string In preparation for config flow. The GatewayId used to be id(gateway). With config flows, every gateway will have its own ConfigEntry. Every ConfigEntry has a unique id. Thus we would have two separate but one-to-one related ID systems. This commit removes this unneeded duplication by using the id of the ConfigEntry as GatewayId. * MySensors: Add unique_id to all entities This allows entities to work well with the frontend. * MySensors: Add device_info to all entities Entities belonging to the same node_id will now by grouped as a device. * MySensors: clean up device.py a bit * MySensors: Add config flow support With this change the MySensors can be fully configured from the GUI. Legacy configuration.yaml configs will be migrated by reading them once. Note that custom node names are not migrated. Users will have to re-enter the names in the front-end. Since there is no straight-forward way to configure global settings, all previously global settings are now per-gateway. These settings include: - MQTT retain - optimistic - persistence enable - MySensors version When a MySensors integration is loaded, it works as follows: 1. __init__.async_setup_entry is called 2. for every platform, async_forward_entry_setup is called 3. the platform's async_setup_entry is called 4. __init__.setup_mysensors_platform is called 5. the entity's constructor (e.g. MySensorsCover) is called 6. the created entity is stored in a dict in the hass object * MySensors: Fix linter errors * MySensors: Remove unused import * MySensors: Feedback from @MartinHjelmare * MySensors: Multi-step config flow * MySensors: More feedback * MySensors: Move all storage in hass object under DOMAIN The integration now stores everything under hass.data["mysensors"] instead of using several top level keys. * MySensors: await shutdown of gateway instead of creating a task * MySensors: Rename Ethernet to TCP * MySensors: Absolute imports and cosmetic changes * MySensors: fix gw_stop * MySensors: Allow user to specify persistence file * MySensors: Nicer log message * MySensors: Add lots of unit tests * MySensors: Fix legacy import of persistence file name Turns out tests help to find bugs :D * MySensors: Improve test coverage * MySensors: Use json persistence files by default * MySensors: Code style improvements * MySensors: Stop adding attributes to existing objects This commit removes the extra attributes that were being added to the gateway objects from pymysensors. Most attributes were easy to remove, except for the gateway id. The MySensorsDevice class needs the gateway id as it is part of its DevId as well as the unique_id and device_info. Most MySensorsDevices actually end up being Entities. Entities have access to their ConfigEntry via self.platform.config_entry. However, the device_tracker platform does not become an Entity. For this reason, the gateway id is not fetched from self.plaform but given as an argument. Additionally, MySensorsDevices expose the address of the gateway (CONF_DEVICE). Entities can easily fetch this information via self.platform, but the device_tracker cannot. This commit chooses to remove the gateway address from device_tracker. While this could in theory break some automations, the simplicity of this solution was deemed worth it. The alternative of adding the entire ConfigEntry as an argument to MySensorsDevices is not viable, because device_tracker is initialized by the async_setup_scanner function that isn't supplied a ConfigEntry. It only gets discovery_info. Adding the entire ConfigEntry doesn't seem appropriate for this edge case. * MySensors: Fix gw_stop and the translations * MySensors: Fix incorrect function calls * MySensors: Fewer comments in const.py * MySensors: Remove union from _get_gateway and remove id from try_connect * MySensors: Deprecate nodes option in configuration.yaml * MySensors: Use version parser from packaging * MySensors: Remove prefix from unique_id and change some private property names * MySensors: Change _get_gateway function signature * MySensors: add packaging==20.8 for the version parser * MySensors: Rename some stuff * MySensors: use pytest.mark.parametrize * MySensors: Clean up test cases * MySensors: Remove unneeded parameter from devices * Revert "MySensors: add packaging==20.8 for the version parser" This reverts commit 6b200ee01a3c0eee98176380bdd0b73e5a25b2dd. * MySensors: Use core interface for testing configuration.yaml import * MySensors: Fix test_init * MySensors: Rename a few variables * MySensors: cosmetic changes * MySensors: Update strings.json * MySensors: Still more feedback from @MartinHjelmare * MySensors: Remove unused strings Co-authored-by: Martin Hjelmare <marhje52@gmail.com> * MySensors: Fix typo and remove another unused string * MySensors: More strings.json * MySensors: Fix gateway ready handler * MySensors: Add duplicate detection to config flows * MySensors: Deal with non-existing topics and ports. Includes unit tests for these cases. * MySensors: Use awesomeversion instead of packaging * Add string already_configured * MySensors: Abort config flow when config is found to be invalid while importing * MySensors: Copy all error messages to also be abort messages All error strings may now also be used as an abort reason, so the strings should be defined * Use string references Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
2021-02-05 22:13:57 +01:00
signal = NODE_CALLBACK.format(gateway_id, msg.node_id)
async_dispatcher_send(hass, signal)