Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade insteonplm to 0.8.2 (required refactoring) #12534

Merged
merged 89 commits into from
Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from 87 commits
Commits
Show all changes
89 commits
Select commit Hold shift + click to select a range
064c893
Merge from current dev
Jan 21, 2018
edb9a6b
Update for Sensor approach
Jan 30, 2018
e000735
Update reference to state classes
Jan 30, 2018
9fb4604
Reference stateKey correctly
Jan 30, 2018
06a0eac
Reference stateKey
Jan 30, 2018
dd346d9
Change deviceInfo to a dict
Jan 30, 2018
9336f3e
Pass state to properties method
Jan 30, 2018
2d4d53a
Add state info to device_state_attributes
Jan 30, 2018
0b2efe1
Update entity name to include state name
Jan 30, 2018
d4e20e7
Update for on() off() vs light_on/off
Jan 31, 2018
0aebd2b
Flag newnames option
Jan 31, 2018
88e8bb5
Update configuration schema
Jan 31, 2018
5a823e8
Update configuration schema
Jan 31, 2018
49afc27
Spell False correctly
Jan 31, 2018
d7cf3ec
Rename state to statekey
Jan 31, 2018
d6e6f06
Rename statekey to stateKey
Jan 31, 2018
69dba29
Call new device with stateKey and newname
Jan 31, 2018
ca1d91d
Simplify use of newnames
Jan 31, 2018
8de7260
Add workdir to save devices
Jan 31, 2018
06e60fd
Fix newnames config setup
Feb 1, 2018
967ed03
Propogate OnOffSensor to VariableSensor change
Feb 1, 2018
6a8936d
Upgrade insteonplm version to 0.8.0
Feb 2, 2018
c7c0534
Pass address rather than device object to platform
Feb 3, 2018
39003dc
Set inteon_plm data variable to PLM
Feb 3, 2018
ae7b8f3
Consistant use of conn and plm
Feb 3, 2018
c51ae4b
Consistant use of conn and plm
Feb 3, 2018
b1c1b2b
Proper reference to device and address
Feb 3, 2018
24c1883
Fixed platform setup issues
Feb 7, 2018
75481d3
Correct issue with missing _supported_features attr
Feb 7, 2018
aad0fa4
Properly reference self._state vs self.state
Feb 8, 2018
7aaf294
Bump insteonplm version to 0.8.1
Feb 8, 2018
9b50e90
Merge from current HA dev branch
Feb 9, 2018
b6b19a9
Remove subplatform and map based on state name
Feb 11, 2018
c05bf26
Correct refrence to State
Feb 11, 2018
b72d015
Correct reference to device.states
Feb 11, 2018
e84ff33
Bump insteonplm to 0.8.2
Feb 17, 2018
6de2339
Merge with latest dev branch
Feb 17, 2018
ab2ee0d
Fix format errors
Feb 17, 2018
13ae2a7
Fix format issues
Feb 17, 2018
c804b5f
Fix trailing whitespace
Feb 17, 2018
dd50660
Correct format issues
Feb 17, 2018
a00c9f6
Fix format issues
Feb 17, 2018
e3f9fde
Fix format issues
Feb 17, 2018
9736890
Fixed format issues
Feb 17, 2018
701ba7b
Fixed format issues
Feb 17, 2018
e87c117
Move imports inside classes
Feb 18, 2018
a3455fd
Simplify import of modules
Feb 18, 2018
6d4701f
Correct reference to OnOffSwitch_OutletTop and bottom
Feb 18, 2018
2810351
Remove unnessary references
Feb 18, 2018
7037b08
Fix format issues
Feb 18, 2018
2a7cc56
Code review adjustments
Feb 18, 2018
6c55846
Fix format issue
Feb 18, 2018
8a2b0c7
Use new nameing format for groups that are not group 0x01
Feb 18, 2018
4cda478
Remove newname feature
Feb 18, 2018
a7a718f
Fix binary_sensor type to default to None
Feb 18, 2018
e077806
Fix device_class property to return the sensor type correctly.
Feb 18, 2018
5bef1fc
rename _device and _state to avoid conflicts with Entity
Feb 19, 2018
15e9eef
Format long lines
Feb 19, 2018
fd3240e
Pylint clean up
Feb 19, 2018
9367a17
Insteon_PLM
Feb 19, 2018
9de6960
lint cleanup
Feb 19, 2018
4a2d320
Check device_override has address
Feb 20, 2018
0d8fb99
3.4 lint clean up
Feb 20, 2018
1836baf
Changes per code review
Feb 21, 2018
c46cca1
Change discovered from a list of dict to a dict
Feb 21, 2018
94b390a
Correct common_attributes usage
Feb 21, 2018
6d11c42
Change discovered from a list of dict to a dict
Feb 21, 2018
65656cc
Add debugging message to confirm platform setup
Feb 21, 2018
92623c4
Debug messages
Feb 21, 2018
0453d9f
Debug messages
Feb 21, 2018
12c5651
Debug async_added_to_hass
Feb 21, 2018
22cadff
Debug async_added_to_hass async_add_job
Feb 21, 2018
4963a25
Debug async_added_to_hass
Feb 21, 2018
07814b4
Debug devices not loading in hass
Feb 21, 2018
55fb724
Debug new entities not being added to hass
Feb 21, 2018
ec30677
Debug adding devices to hass
Feb 21, 2018
4bd0832
Revert "3.4 lint clean up"
Feb 21, 2018
96baad0
3.4 lint clean up
Feb 20, 2018
5ef044a
Revert "Debug adding devices to hass"
Feb 21, 2018
d217668
Revert "Debug new entities not being added to hass"
Feb 21, 2018
452619f
Revert "Debug devices not loading in hass"
Feb 21, 2018
9fcdfc5
Revert "Debug async_added_to_hass"
Feb 21, 2018
1e5ddc2
Revert "Debug async_added_to_hass async_add_job"
Feb 21, 2018
8819294
Revert "Debug async_added_to_hass"
Feb 21, 2018
39046c9
Pylint clean up
Feb 23, 2018
e17f2f1
pylint cleanup
Feb 23, 2018
36230de
Clean up naming
Feb 24, 2018
a0b7c25
Enhance config schema. Fix logging issue
Feb 25, 2018
22b69fd
Reapply changes after squash
Feb 25, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 25 additions & 55 deletions homeassistant/components/binary_sensor/insteon_plm.py
Original file line number Diff line number Diff line change
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.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't end the log message with a period.

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."""
InsteonPLMEntity.__init__(self, device, state_key)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
Original file line number Diff line number Diff line change
@@ -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.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove period at the end.

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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The methods should be annotated with @asyncio.coroutine.

"""Turn on the entity."""
if speed is None:
speed = SPEED_MEDIUM
self.async_set_speed(speed)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yield from this coroutine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be addressed.


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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be a coroutine so yield from it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yield from self.async_set_speed(...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhat related question. async_setup_platform passes in an add device handler (in this code it is called async_add_devices. Should that be yield from?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, async_add_devices is not a coroutine.


@asyncio.coroutine
def async_set_speed(self, speed: str) -> None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be annotated with @asyncio.coroutine.

"""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
Loading