Simplify maxcube integration (#48403)

* Simplify maxcube integration

Device objects returned by maxcube-api dependency are stable, so
we do not need to resolve from the device address every time.

Also, refactor and unify how maxcube integration sets temperature & mode.

* Raise ValueError if missing parameters for set_temperature method

Raise a ValueError exception If set_temperature does not receive
a temperature parameter.

Also, document properly _set_target method.

* Use Type | None instead of Optional[Type] annotation

* Protect set_hvac_mode and set_preset_mode from unsupported parameters
This commit is contained in:
Unai 2021-03-28 00:21:20 +01:00 committed by GitHub
parent ffdfc521b9
commit 0706ae70dc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 134 additions and 129 deletions

View file

@ -11,13 +11,10 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
"""Iterate through all MAX! Devices and add window shutters."""
devices = []
for handler in hass.data[DATA_KEY].values():
cube = handler.cube
for device in cube.devices:
name = f"{cube.room_by_id(device.room_id).name} {device.name}"
for device in handler.cube.devices:
# Only add Window Shutters
if device.is_windowshutter():
devices.append(MaxCubeShutter(handler, name, device.rf_address))
devices.append(MaxCubeShutter(handler, device))
if devices:
add_entities(devices)
@ -26,13 +23,12 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class MaxCubeShutter(BinarySensorEntity):
"""Representation of a MAX! Cube Binary Sensor device."""
def __init__(self, handler, name, rf_address):
def __init__(self, handler, device):
"""Initialize MAX! Cube BinarySensorEntity."""
self._name = name
self._sensor_type = DEVICE_CLASS_WINDOW
self._rf_address = rf_address
room = handler.cube.room_by_id(device.room_id)
self._name = f"{room.name} {device.name}"
self._cubehandle = handler
self._state = None
self._device = device
@property
def name(self):
@ -42,15 +38,13 @@ class MaxCubeShutter(BinarySensorEntity):
@property
def device_class(self):
"""Return the class of this sensor."""
return self._sensor_type
return DEVICE_CLASS_WINDOW
@property
def is_on(self):
"""Return true if the binary sensor is on/open."""
return self._state
return self._device.is_open
def update(self):
"""Get latest data from MAX! Cube."""
self._cubehandle.update()
device = self._cubehandle.cube.device_by_rf(self._rf_address)
self._state = device.is_open