mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Refactor and update to use latest pymysensors.
This commit is contained in:
parent
e87652e95f
commit
c41d7b8f6d
@ -1,77 +1,93 @@
|
|||||||
import homeassistant.external.pymysensors.mysensors as mysensors
|
"""
|
||||||
from homeassistant.helpers.entity import Entity
|
homeassistant.components.sensor.mysensors
|
||||||
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
MySensors sensors.
|
||||||
|
|
||||||
|
Config:
|
||||||
|
sensor:
|
||||||
|
- platform: mysensors
|
||||||
|
port: '/dev/ttyACM0'
|
||||||
|
"""
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
#
|
# pylint: disable=no-name-in-module, import-error
|
||||||
# Config:
|
import homeassistant.external.pymysensors.mysensors.mysensors as mysensors
|
||||||
# sensor:
|
import homeassistant.external.pymysensors.mysensors.const as const
|
||||||
# platform: mysensors
|
from homeassistant.helpers.entity import Entity
|
||||||
# gateway: serial
|
|
||||||
# port: '/dev/ttyACM0'
|
|
||||||
#
|
|
||||||
|
|
||||||
from homeassistant.const import (
|
from homeassistant.const import ATTR_BATTERY_LEVEL
|
||||||
ATTR_BATTERY_LEVEL )
|
|
||||||
|
CONF_PORT = "port"
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
devices = {} # keep track of devices added to HA
|
|
||||||
gw = mysensors.Gateway();
|
|
||||||
|
|
||||||
def setup_platform(hass, config, add_devices, discovery_info=None):
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
||||||
|
""" Setup the mysensors platform. """
|
||||||
|
|
||||||
# Passed to pymysensors and called when a sensor is updated
|
devices = {} # keep track of devices added to HA
|
||||||
def sensor_update(type, nid, cid = None, value = None):
|
|
||||||
s = gw.sensors[nid]
|
|
||||||
if s.sketch_name is not None:
|
|
||||||
if nid in devices:
|
|
||||||
devices[nid]._battery_level = s.battery_level
|
|
||||||
for c in s.children:
|
|
||||||
child = s.children[c]
|
|
||||||
devices[nid]._children[child.id] = MySensorsChildSensor(child.type, child.value)
|
|
||||||
else:
|
|
||||||
devices[nid] = MySensorsSensor(s.sketch_name)
|
|
||||||
add_devices([devices[nid]])
|
|
||||||
|
|
||||||
if config['gateway'] == 'serial':
|
def sensor_update(update_type, nid):
|
||||||
gw = mysensors.SerialGateway('/dev/ttyACM0', sensor_update)
|
""" Callback for sensor updates from the MySensors gateway. """
|
||||||
else:
|
sensor = gateway.sensors[nid]
|
||||||
_LOGGER.error('mysensors gateway type: ' + config.gateway + ' not supported')
|
if sensor.sketch_name is None:
|
||||||
|
return
|
||||||
|
if nid not in devices:
|
||||||
|
devices[nid] = MySensorsNode(sensor.sketch_name)
|
||||||
|
add_devices([devices[nid]])
|
||||||
|
|
||||||
gw.listen()
|
devices[nid].battery_level = sensor.battery_level
|
||||||
|
for child_id, child in sensor.children.items():
|
||||||
|
devices[nid].update_child(child_id, child)
|
||||||
|
|
||||||
|
port = config.get(CONF_PORT)
|
||||||
|
if port is None:
|
||||||
|
_LOGGER.error("Missing required key 'port'")
|
||||||
|
return False
|
||||||
|
|
||||||
|
gateway = mysensors.SerialGateway(port, sensor_update)
|
||||||
|
gateway.start()
|
||||||
|
|
||||||
|
|
||||||
class MySensorsSensor(Entity):
|
class MySensorsNode(Entity):
|
||||||
|
""" Represents a MySensors node. """
|
||||||
def __init__(self, name):
|
def __init__(self, name):
|
||||||
self._name = name
|
self._name = name
|
||||||
self._state = ''
|
self.battery_level = 0
|
||||||
self._battery_level = 0
|
self.children = {}
|
||||||
self._children = {}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
|
""" The name of this sensor. """
|
||||||
return self._name
|
return self._name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state(self):
|
def state(self):
|
||||||
""" Returns the state of the device. """
|
""" Returns the state of the device. """
|
||||||
return self._state
|
return ''
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def state_attributes(self):
|
def state_attributes(self):
|
||||||
""" Returns the state attributes. """
|
""" Returns the state attributes. """
|
||||||
attrs = {}
|
attrs = {}
|
||||||
attrs[ATTR_BATTERY_LEVEL] = self._battery_level
|
attrs[ATTR_BATTERY_LEVEL] = self.battery_level
|
||||||
|
|
||||||
for c in self._children.values():
|
|
||||||
attrs[c._type] = c._value
|
|
||||||
|
|
||||||
|
for child in self.children.values():
|
||||||
|
for value_type, value in child.values.items():
|
||||||
|
attrs[value_type] = value
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
#def update(self):
|
def update_child(self, child_id, child):
|
||||||
# get latest values from gateway
|
""" Sets the values of a child sensor. """
|
||||||
|
self.children[child_id] = MySensorsChildSensor(
|
||||||
|
const.Presentation(child.type).name,
|
||||||
|
{const.SetReq(t).name: v for t, v in child.values.items()})
|
||||||
|
|
||||||
|
|
||||||
class MySensorsChildSensor():
|
class MySensorsChildSensor():
|
||||||
def __init__(self, type, value):
|
""" Represents a MySensors child sensor. """
|
||||||
self._type = type
|
# pylint: disable=too-few-public-methods
|
||||||
self._value = value
|
def __init__(self, child_type, values):
|
||||||
|
self.type = child_type
|
||||||
|
self.values = values
|
||||||
|
2
homeassistant/external/pymysensors
vendored
2
homeassistant/external/pymysensors
vendored
@ -1 +1 @@
|
|||||||
Subproject commit 9b86a46c245630e30149ba4e3b0729ef2803a702
|
Subproject commit b3937f3ac15f47f57ed0cfb95709a77b7b7bed06
|
Loading…
x
Reference in New Issue
Block a user