hass-core/homeassistant/components/notify/mysensors.py
Martin Hjelmare be3b227a87
Make mysensors component async ()
* Make mysensors component async

* Use async dispatcher and discovery.
* Run I/O in executor.
* Make mysensors actuator methods async.
* Upgrade pymysensors to 0.13.0.
* Use async serial gateway.
* Use async TCP gateway.
* Use async mqtt gateway.

* Start gateway before hass start event

* Make sure gateway is started after discovery of persistent devices
  and after corresponding platforms have been loaded.
* Don't wait to start gateway until after hass start.

* Bump pymysensors to 0.14.0
2018-05-11 09:39:18 +02:00

52 lines
1.8 KiB
Python

"""
MySensors notification service.
For more details about this platform, please refer to the documentation
https://home-assistant.io/components/notify.mysensors/
"""
from homeassistant.components import mysensors
from homeassistant.components.notify import (
ATTR_TARGET, DOMAIN, BaseNotificationService)
async def async_get_service(hass, config, discovery_info=None):
"""Get the MySensors notification service."""
new_devices = mysensors.setup_mysensors_platform(
hass, DOMAIN, discovery_info, MySensorsNotificationDevice)
if not new_devices:
return None
return MySensorsNotificationService(hass)
class MySensorsNotificationDevice(mysensors.MySensorsDevice):
"""Represent a MySensors Notification device."""
def send_msg(self, msg):
"""Send a message."""
for sub_msg in [msg[i:i + 25] for i in range(0, len(msg), 25)]:
# Max mysensors payload is 25 bytes.
self.gateway.set_child_value(
self.node_id, self.child_id, self.value_type, sub_msg)
def __repr__(self):
"""Return the representation."""
return "<MySensorsNotificationDevice {}>".format(self.name)
class MySensorsNotificationService(BaseNotificationService):
"""Implement a MySensors notification service."""
# pylint: disable=too-few-public-methods
def __init__(self, hass):
"""Initialize the service."""
self.devices = mysensors.get_mysensors_devices(hass, DOMAIN)
async def async_send_message(self, message="", **kwargs):
"""Send a message to a user."""
target_devices = kwargs.get(ATTR_TARGET)
devices = [device for device in self.devices.values()
if target_devices is None or device.name in target_devices]
for device in devices:
device.send_msg(message)