diff --git a/homeassistant/components/mysensors.py b/homeassistant/components/mysensors.py index 59724a7d810..1dcd65ab8e6 100644 --- a/homeassistant/components/mysensors.py +++ b/homeassistant/components/mysensors.py @@ -68,11 +68,9 @@ def setup(hass, config): # noqa IS_METRIC = (hass.config.temperature_unit == TEMP_CELCIUS) def callback_generator(port, devices): - """ - Generator of callback, should be run once per gateway setup. - """ + """Return a new callback function. Run once per gateway setup.""" def node_update(update_type, nid): - """ Callback for node updates from the MySensors gateway. """ + """Callback for node updates from the MySensors gateway.""" _LOGGER.info('update %s: node %s', update_type, nid) hass.bus.fire(EVENT_MYSENSORS_NODE_UPDATE, { @@ -85,11 +83,7 @@ def setup(hass, config): # noqa return node_update def setup_gateway(port, persistence, persistence_file): - """ - Instantiate gateway, set gateway attributes and start gateway. - If persistence is true, update all nodes. - Listen for stop of home-assistant, then stop gateway. - """ + """Return gateway after setup of the gateway.""" devices = {} # keep track of devices added to HA gateway = mysensors.SerialGateway(port, persistence=persistence, @@ -137,10 +131,7 @@ def setup(hass, config): # noqa def mysensors_update(platform_type): - """ - Decorator for callback function for sensor updates from the MySensors - component. - """ + """Decorator for callback function for mysensor updates.""" def wrapper(gateway, port, devices, nid): """Wrapper function in the decorator.""" sensor = gateway.sensors[nid] diff --git a/homeassistant/components/sensor/mysensors.py b/homeassistant/components/sensor/mysensors.py index c16980f7587..f1ce4f38271 100644 --- a/homeassistant/components/sensor/mysensors.py +++ b/homeassistant/components/sensor/mysensors.py @@ -22,8 +22,7 @@ DEPENDENCIES = ['mysensors'] def setup_platform(hass, config, add_devices, discovery_info=None): - """ Setup the mysensors platform for sensors. """ - + """Setup the mysensors platform for sensors.""" # Define the V_TYPES that the platform should handle as states. v_types = [] for _, member in mysensors.CONST.SetReq.__members__.items(): @@ -39,7 +38,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return (v_types, MySensorsSensor, add_devices) def sensor_update(event): - """ Callback for sensor updates from the MySensors component. """ + """Callback for sensor updates from the MySensors component.""" _LOGGER.info( 'update %s: node %s', event.data[mysensors.ATTR_UPDATE_TYPE], event.data[mysensors.ATTR_NODE_ID]) @@ -52,21 +51,39 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class MySensorsSensor(Entity): + """Represent the value of a MySensors child node.""" - """ Represents the value of a MySensors child node. """ # pylint: disable=too-many-arguments, too-many-instance-attributes def __init__(self, port, node_id, child_id, name, value_type): + """Setup class attributes on instantiation. + + Args: + port (str): Gateway port. + node_id (str): Id of node. + child_id (str): Id of child. + name (str): Sketch name. + value_type (str): Value type of child. Value is entity state. + + Attributes: + port (str): Gateway port. + node_id (str): Id of node. + child_id (str): Id of child. + _name (str): Sketch name. + value_type (str): Value type of child. Value is entity state. + battery_level (int): Node battery level. + _values (dict): Child values. Non state values set as state attributes. + """ self.port = port - self._name = name self.node_id = node_id self.child_id = child_id - self.battery_level = 0 + self._name = name self.value_type = value_type + self.battery_level = 0 self._values = {} def as_dict(self): - """ Returns a dict representation of this Entity. """ + """Return a dict representation of this Entity.""" return { 'port': self.port, 'name': self._name, @@ -79,24 +96,24 @@ class MySensorsSensor(Entity): @property def should_poll(self): - """ MySensor gateway pushes its state to HA. """ + """MySensor gateway pushes its state to HA.""" return False @property def name(self): - """ The name of this sensor. """ + """The name of this sensor.""" return self._name @property def state(self): - """ Returns the state of the device. """ + """Return the state of the device.""" if not self._values: return '' return self._values[self.value_type] @property def unit_of_measurement(self): - """ Unit of measurement of this entity. """ + """Unit of measurement of this entity.""" # pylint:disable=too-many-return-statements if self.value_type == mysensors.CONST.SetReq.V_TEMP: return TEMP_CELCIUS if mysensors.IS_METRIC else TEMP_FAHRENHEIT @@ -121,15 +138,14 @@ class MySensorsSensor(Entity): @property def device_state_attributes(self): - """ Returns device specific state attributes. """ + """Return device specific state attributes.""" device_attr = dict(self._values) device_attr.pop(self.value_type, None) return device_attr @property def state_attributes(self): - """ Returns the state attributes. """ - + """Return the state attributes.""" data = { mysensors.ATTR_NODE_ID: self.node_id, mysensors.ATTR_CHILD_ID: self.child_id, @@ -144,7 +160,7 @@ class MySensorsSensor(Entity): return data def update_sensor(self, values, battery_level): - """ Update the controller with the latest values from a sensor. """ + """Update the controller with the latest values from a sensor.""" for value_type, value in values.items(): _LOGGER.info( "%s: value_type %s, value = %s", self._name, value_type, value) diff --git a/homeassistant/components/switch/mysensors.py b/homeassistant/components/switch/mysensors.py index 5db5b9d25fc..541c305fafa 100644 --- a/homeassistant/components/switch/mysensors.py +++ b/homeassistant/components/switch/mysensors.py @@ -22,8 +22,7 @@ DEPENDENCIES = ['mysensors'] def setup_platform(hass, config, add_devices, discovery_info=None): - """ Setup the mysensors platform for switches. """ - + """Setup the mysensors platform for switches.""" # Define the V_TYPES that the platform should handle as states. v_types = [] for _, member in mysensors.CONST.SetReq.__members__.items(): @@ -39,7 +38,7 @@ def setup_platform(hass, config, add_devices, discovery_info=None): return (v_types, MySensorsSwitch, add_devices) def sensor_update(event): - """ Callback for sensor updates from the MySensors component. """ + """Callback for sensor updates from the MySensors component.""" _LOGGER.info( 'update %s: node %s', event.data[mysensors.ATTR_UPDATE_TYPE], event.data[mysensors.ATTR_NODE_ID]) @@ -52,21 +51,39 @@ def setup_platform(hass, config, add_devices, discovery_info=None): class MySensorsSwitch(SwitchDevice): + """Represent the value of a MySensors child node.""" - """ Represents the value of a MySensors child node. """ # pylint: disable=too-many-arguments, too-many-instance-attributes def __init__(self, port, node_id, child_id, name, value_type): + """Setup class attributes on instantiation. + + Args: + port (str): Gateway port. + node_id (str): Id of node. + child_id (str): Id of child. + name (str): Sketch name. + value_type (str): Value type of child. Value is entity state. + + Attributes: + port (str): Gateway port. + node_id (str): Id of node. + child_id (str): Id of child. + _name (str): Sketch name. + value_type (str): Value type of child. Value is entity state. + battery_level (int): Node battery level. + _values (dict): Child values. Non state values set as state attributes. + """ self.port = port - self._name = name self.node_id = node_id self.child_id = child_id - self.battery_level = 0 + self._name = name self.value_type = value_type + self.battery_level = 0 self._values = {} def as_dict(self): - """ Returns a dict representation of this Entity. """ + """Return a dict representation of this Entity.""" return { 'port': self.port, 'name': self._name, @@ -79,17 +96,17 @@ class MySensorsSwitch(SwitchDevice): @property def should_poll(self): - """ MySensor gateway pushes its state to HA. """ + """MySensor gateway pushes its state to HA.""" return False @property def name(self): - """ The name of this sensor. """ + """The name of this sensor.""" return self._name @property def unit_of_measurement(self): - """ Unit of measurement of this entity. """ + """Unit of measurement of this entity.""" # pylint:disable=too-many-return-statements if self.value_type == mysensors.CONST.SetReq.V_TEMP: return TEMP_CELCIUS if mysensors.IS_METRIC else TEMP_FAHRENHEIT @@ -114,15 +131,14 @@ class MySensorsSwitch(SwitchDevice): @property def device_state_attributes(self): - """ Returns device specific state attributes. """ + """Return device specific state attributes.""" device_attr = dict(self._values) device_attr.pop(self.value_type, None) return device_attr @property def state_attributes(self): - """ Returns the state attributes. """ - + """Return the state attributes.""" data = { mysensors.ATTR_NODE_ID: self.node_id, mysensors.ATTR_CHILD_ID: self.child_id, @@ -138,25 +154,27 @@ class MySensorsSwitch(SwitchDevice): @property def is_on(self): - """ Returns True if switch is on. """ - return self._values[self.value_type] == STATE_ON + """Return True if switch is on.""" + if self.value_type in self._values: + return self._values[self.value_type] == STATE_ON + return False def turn_on(self): - """ Turns the switch on. """ + """Turn the switch on.""" mysensors.GATEWAYS[self.port].set_child_value( self.node_id, self.child_id, self.value_type, 1) self._values[self.value_type] = STATE_ON self.update_ha_state() def turn_off(self): - """ Turns the pin to low/off. """ + """Turn the pin to low/off.""" mysensors.GATEWAYS[self.port].set_child_value( self.node_id, self.child_id, self.value_type, 0) self._values[self.value_type] = STATE_OFF self.update_ha_state() def update_sensor(self, values, battery_level): - """ Update the controller with the latest value from a sensor. """ + """Update the controller with the latest value from a sensor.""" for value_type, value in values.items(): _LOGGER.info( "%s: value_type %s, value = %s", self._name, value_type, value)