Skip to content

Commit

Permalink
Upgrade insteonplm to 0.8.2 (required refactoring) (#12534)
Browse files Browse the repository at this point in the history
* Merge from current dev

* Update for Sensor approach

* Update reference to state classes

* Reference stateKey correctly

* Reference stateKey

* Change deviceInfo to a dict

* Pass state to properties method

* Add state info to device_state_attributes

* Update entity name to include state name

* Update for on() off() vs light_on/off

* Flag newnames option

* Update configuration schema

* Update configuration schema

* Spell False correctly

* Rename state to statekey

* Rename statekey to stateKey

* Call new device with stateKey and newname

* Simplify use of newnames

* Add workdir to save devices

* Fix newnames config setup

* Propogate OnOffSensor to VariableSensor change

* Upgrade insteonplm version to 0.8.0

* Pass address rather than device object to platform

* Set inteon_plm data variable to PLM

* Consistant use of conn and plm

* Consistant use of conn and plm

* Proper reference to device and address

* Fixed platform setup issues

* Correct issue with missing _supported_features attr

* Properly reference self._state vs self.state

* Bump insteonplm version to 0.8.1

* Remove subplatform and map based on state name

* Correct refrence to State

* Correct reference to device.states

* Bump insteonplm to 0.8.2

* Fix format errors

* Fix format issues

* Fix trailing whitespace

* Correct format issues

* Fix format issues

* Fix format issues

* Fixed format issues

* Fixed format issues

* Move imports inside classes

* Simplify import of modules

* Correct reference to OnOffSwitch_OutletTop and bottom

* Remove unnessary references

* Fix format issues

* Code review adjustments

* Fix format issue

* Use new nameing format for groups that are not group 0x01

* Remove newname feature

* Fix binary_sensor type to default to None

* Fix device_class property to return the sensor type correctly.

* rename _device and _state to avoid conflicts with Entity

* Format long lines

* Pylint clean up

* Insteon_PLM

* lint cleanup

* Check device_override has address

* 3.4 lint clean up

* Changes per code review

* Change discovered from a list of dict to a dict

* Correct common_attributes usage

* Change discovered from a list of dict to a dict

* Add debugging message to confirm platform setup

* Debug messages

* Debug messages

* Debug async_added_to_hass

* Debug async_added_to_hass async_add_job

* Debug async_added_to_hass

* Debug devices not loading in hass

* Debug new entities not being added to hass

* Debug adding devices to hass

* Revert "3.4 lint clean up"

This reverts commit 0d8fb99.

* 3.4 lint clean up

* Revert "Debug adding devices to hass"

This reverts commit ec30677.

* Revert "Debug new entities not being added to hass"

This reverts commit 55fb724.

* Revert "Debug devices not loading in hass"

This reverts commit 07814b4.

* Revert "Debug async_added_to_hass"

This reverts commit 4963a25.

* Revert "Debug async_added_to_hass async_add_job"

This reverts commit 22cadff.

* Revert "Debug async_added_to_hass"

This reverts commit 12c5651.

* Pylint clean up

* pylint cleanup

* Clean up naming

* Enhance config schema. Fix logging issue

* Reapply changes after squash
  • Loading branch information
teharris1 authored and MartinHjelmare committed Feb 25, 2018
1 parent e8173fb commit 32166fd
Show file tree
Hide file tree
Showing 7 changed files with 371 additions and 249 deletions.
80 changes: 25 additions & 55 deletions homeassistant/components/binary_sensor/insteon_plm.py
Expand Up @@ -2,86 +2,56 @@
Support for INSTEON dimmers via PowerLinc Modem.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/insteon_plm/
https://home-assistant.io/components/binary_sensor.insteon_plm/
"""
import logging
import asyncio
import logging

from homeassistant.core import callback
from homeassistant.components.binary_sensor import BinarySensorDevice
from homeassistant.loader import get_component
from homeassistant.components.insteon_plm import InsteonPLMEntity

DEPENDENCIES = ['insteon_plm']

_LOGGER = logging.getLogger(__name__)

SENSOR_TYPES = {'openClosedSensor': 'opening',
'motionSensor': 'motion',
'doorSensor': 'door',
'leakSensor': 'moisture'}


@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up the INSTEON PLM device class for the hass platform."""
plm = hass.data['insteon_plm']

device_list = []
for device in discovery_info:
name = device.get('address')
address = device.get('address_hex')

_LOGGER.info('Registered %s with binary_sensor platform.', name)

device_list.append(
InsteonPLMBinarySensorDevice(hass, plm, address, name)
)
address = discovery_info['address']
device = plm.devices[address]
state_key = discovery_info['state_key']

async_add_devices(device_list)
_LOGGER.debug('Adding device %s entity %s to Binary Sensor platform',
device.address.hex, device.states[state_key].name)

new_entity = InsteonPLMBinarySensor(device, state_key)

class InsteonPLMBinarySensorDevice(BinarySensorDevice):
"""A Class for an Insteon device."""
async_add_devices([new_entity])

def __init__(self, hass, plm, address, name):
"""Initialize the binarysensor."""
self._hass = hass
self._plm = plm.protocol
self._address = address
self._name = name

self._plm.add_update_callback(
self.async_binarysensor_update, {'address': self._address})
class InsteonPLMBinarySensor(InsteonPLMEntity, BinarySensorDevice):
"""A Class for an Insteon device entity."""

@property
def should_poll(self):
"""No polling needed."""
return False

@property
def address(self):
"""Return the address of the node."""
return self._address
def __init__(self, device, state_key):
"""Initialize the INSTEON PLM binary sensor."""
super().__init__(device, state_key)
self._sensor_type = SENSOR_TYPES.get(self._insteon_device_state.name)

@property
def name(self):
"""Return the name of the node."""
return self._name
def device_class(self):
"""Return the class of this sensor."""
return self._sensor_type

@property
def is_on(self):
"""Return the boolean response if the node is on."""
sensorstate = self._plm.get_device_attr(self._address, 'sensorstate')
_LOGGER.info("Sensor state for %s is %s", self._address, sensorstate)
sensorstate = self._insteon_device_state.value
return bool(sensorstate)

@property
def device_state_attributes(self):
"""Provide attributes for display on device card."""
insteon_plm = get_component('insteon_plm')
return insteon_plm.common_attributes(self)

def get_attr(self, key):
"""Return specified attribute for this device."""
return self._plm.get_device_attr(self.address, key)

@callback
def async_binarysensor_update(self, message):
"""Receive notification from transport that new data exists."""
_LOGGER.info("Received update callback from PLM for %s", self._address)
self._hass.async_add_job(self.async_update_ha_state())
96 changes: 96 additions & 0 deletions homeassistant/components/fan/insteon_plm.py
@@ -0,0 +1,96 @@
"""
Support for INSTEON fans via PowerLinc Modem.
For more details about this component, please refer to the documentation at
https://home-assistant.io/components/fan.insteon_plm/
"""
import asyncio
import logging

from homeassistant.components.fan import (SPEED_OFF,
SPEED_LOW,
SPEED_MEDIUM,
SPEED_HIGH,
FanEntity,
SUPPORT_SET_SPEED)
from homeassistant.const import STATE_OFF
from homeassistant.components.insteon_plm import InsteonPLMEntity

DEPENDENCIES = ['insteon_plm']

SPEED_TO_HEX = {SPEED_OFF: 0x00,
SPEED_LOW: 0x3f,
SPEED_MEDIUM: 0xbe,
SPEED_HIGH: 0xff}

FAN_SPEEDS = [STATE_OFF, SPEED_LOW, SPEED_MEDIUM, SPEED_HIGH]

_LOGGER = logging.getLogger(__name__)


@asyncio.coroutine
def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
"""Set up the INSTEON PLM device class for the hass platform."""
plm = hass.data['insteon_plm']

address = discovery_info['address']
device = plm.devices[address]
state_key = discovery_info['state_key']

_LOGGER.debug('Adding device %s entity %s to Fan platform',
device.address.hex, device.states[state_key].name)

new_entity = InsteonPLMFan(device, state_key)

async_add_devices([new_entity])


class InsteonPLMFan(InsteonPLMEntity, FanEntity):
"""An INSTEON fan component."""

@property
def speed(self) -> str:
"""Return the current speed."""
return self._hex_to_speed(self._insteon_device_state.value)

@property
def speed_list(self) -> list:
"""Get the list of available speeds."""
return FAN_SPEEDS

@property
def supported_features(self) -> int:
"""Flag supported features."""
return SUPPORT_SET_SPEED

@asyncio.coroutine
def async_turn_on(self, speed: str = None, **kwargs) -> None:
"""Turn on the entity."""
if speed is None:
speed = SPEED_MEDIUM
yield from self.async_set_speed(speed)

@asyncio.coroutine
def async_turn_off(self, **kwargs) -> None:
"""Turn off the entity."""
yield from self.async_set_speed(SPEED_OFF)

@asyncio.coroutine
def async_set_speed(self, speed: str) -> None:
"""Set the speed of the fan."""
fan_speed = SPEED_TO_HEX[speed]
if fan_speed == 0x00:
self._insteon_device_state.off()
else:
self._insteon_device_state.set_level(fan_speed)

@staticmethod
def _hex_to_speed(speed: int):
hex_speed = SPEED_OFF
if speed > 0xfe:
hex_speed = SPEED_HIGH
elif speed > 0x7f:
hex_speed = SPEED_MEDIUM
elif speed > 0:
hex_speed = SPEED_LOW
return hex_speed

0 comments on commit 32166fd

Please sign in to comment.