Clean up freedompro (#52992)

This commit is contained in:
Robert Hillis 2021-07-15 03:27:31 -04:00 committed by GitHub
parent 82256b2588
commit 5ff9c3e611
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 41 deletions

View File

@ -6,9 +6,7 @@ from homeassistant.components.binary_sensor import (
DEVICE_CLASS_SMOKE, DEVICE_CLASS_SMOKE,
BinarySensorEntity, BinarySensorEntity,
) )
from homeassistant.const import CONF_API_KEY
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN from .const import DOMAIN
@ -32,10 +30,9 @@ SUPPORTED_SENSORS = {"smokeSensor", "occupancySensor", "motionSensor", "contactS
async def async_setup_entry(hass, entry, async_add_entities): async def async_setup_entry(hass, entry, async_add_entities):
"""Set up Freedompro binary_sensor.""" """Set up Freedompro binary_sensor."""
api_key = entry.data[CONF_API_KEY]
coordinator = hass.data[DOMAIN][entry.entry_id] coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities( async_add_entities(
Device(hass, api_key, device, coordinator) Device(device, coordinator)
for device in coordinator.data for device in coordinator.data
if device["type"] in SUPPORTED_SENSORS if device["type"] in SUPPORTED_SENSORS
) )
@ -44,25 +41,21 @@ async def async_setup_entry(hass, entry, async_add_entities):
class Device(CoordinatorEntity, BinarySensorEntity): class Device(CoordinatorEntity, BinarySensorEntity):
"""Representation of an Freedompro binary_sensor.""" """Representation of an Freedompro binary_sensor."""
def __init__(self, hass, api_key, device, coordinator): def __init__(self, device, coordinator):
"""Initialize the Freedompro binary_sensor.""" """Initialize the Freedompro binary_sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self._hass = hass
self._session = aiohttp_client.async_get_clientsession(self._hass)
self._api_key = api_key
self._attr_name = device["name"] self._attr_name = device["name"]
self._attr_unique_id = device["uid"] self._attr_unique_id = device["uid"]
self._type = device["type"] self._type = device["type"]
self._characteristics = device["characteristics"]
self._attr_device_info = { self._attr_device_info = {
"name": self.name, "name": self.name,
"identifiers": { "identifiers": {
(DOMAIN, self.unique_id), (DOMAIN, self.unique_id),
}, },
"model": self._type, "model": device["type"],
"manufacturer": "Freedompro", "manufacturer": "Freedompro",
} }
self._attr_device_class = DEVICE_CLASS_MAP[self._type] self._attr_device_class = DEVICE_CLASS_MAP[device["type"]]
@callback @callback
def _handle_coordinator_update(self) -> None: def _handle_coordinator_update(self) -> None:

View File

@ -36,27 +36,24 @@ class Device(CoordinatorEntity, LightEntity):
def __init__(self, hass, api_key, device, coordinator): def __init__(self, hass, api_key, device, coordinator):
"""Initialize the Freedompro light.""" """Initialize the Freedompro light."""
super().__init__(coordinator) super().__init__(coordinator)
self._hass = hass self._session = aiohttp_client.async_get_clientsession(hass)
self._session = aiohttp_client.async_get_clientsession(self._hass)
self._api_key = api_key self._api_key = api_key
self._attr_name = device["name"] self._attr_name = device["name"]
self._attr_unique_id = device["uid"] self._attr_unique_id = device["uid"]
self._type = device["type"]
self._characteristics = device["characteristics"]
self._attr_device_info = { self._attr_device_info = {
"name": self.name, "name": self.name,
"identifiers": { "identifiers": {
(DOMAIN, self.unique_id), (DOMAIN, self.unique_id),
}, },
"model": self._type, "model": device["type"],
"manufacturer": "Freedompro", "manufacturer": "Freedompro",
} }
self._attr_is_on = False self._attr_is_on = False
self._attr_brightness = 0 self._attr_brightness = 0
color_mode = COLOR_MODE_ONOFF color_mode = COLOR_MODE_ONOFF
if "hue" in self._characteristics: if "hue" in device["characteristics"]:
color_mode = COLOR_MODE_HS color_mode = COLOR_MODE_HS
elif "brightness" in self._characteristics: elif "brightness" in device["characteristics"]:
color_mode = COLOR_MODE_BRIGHTNESS color_mode = COLOR_MODE_BRIGHTNESS
self._attr_color_mode = color_mode self._attr_color_mode = color_mode
self._attr_supported_color_modes = {color_mode} self._attr_supported_color_modes = {color_mode}

View File

@ -6,9 +6,8 @@ from homeassistant.components.sensor import (
STATE_CLASS_MEASUREMENT, STATE_CLASS_MEASUREMENT,
SensorEntity, SensorEntity,
) )
from homeassistant.const import CONF_API_KEY, LIGHT_LUX, PERCENTAGE, TEMP_CELSIUS from homeassistant.const import LIGHT_LUX, PERCENTAGE, TEMP_CELSIUS
from homeassistant.core import callback from homeassistant.core import callback
from homeassistant.helpers import aiohttp_client
from homeassistant.helpers.update_coordinator import CoordinatorEntity from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .const import DOMAIN from .const import DOMAIN
@ -38,10 +37,9 @@ SUPPORTED_SENSORS = {"temperatureSensor", "humiditySensor", "lightSensor"}
async def async_setup_entry(hass, entry, async_add_entities): async def async_setup_entry(hass, entry, async_add_entities):
"""Set up Freedompro sensor.""" """Set up Freedompro sensor."""
api_key = entry.data[CONF_API_KEY]
coordinator = hass.data[DOMAIN][entry.entry_id] coordinator = hass.data[DOMAIN][entry.entry_id]
async_add_entities( async_add_entities(
Device(hass, api_key, device, coordinator) Device(device, coordinator)
for device in coordinator.data for device in coordinator.data
if device["type"] in SUPPORTED_SENSORS if device["type"] in SUPPORTED_SENSORS
) )
@ -50,27 +48,23 @@ async def async_setup_entry(hass, entry, async_add_entities):
class Device(CoordinatorEntity, SensorEntity): class Device(CoordinatorEntity, SensorEntity):
"""Representation of an Freedompro sensor.""" """Representation of an Freedompro sensor."""
def __init__(self, hass, api_key, device, coordinator): def __init__(self, device, coordinator):
"""Initialize the Freedompro sensor.""" """Initialize the Freedompro sensor."""
super().__init__(coordinator) super().__init__(coordinator)
self._hass = hass
self._session = aiohttp_client.async_get_clientsession(self._hass)
self._api_key = api_key
self._attr_name = device["name"] self._attr_name = device["name"]
self._attr_unique_id = device["uid"] self._attr_unique_id = device["uid"]
self._type = device["type"] self._type = device["type"]
self._characteristics = device["characteristics"]
self._attr_device_info = { self._attr_device_info = {
"name": self.name, "name": self.name,
"identifiers": { "identifiers": {
(DOMAIN, self.unique_id), (DOMAIN, self.unique_id),
}, },
"model": self._type, "model": device["type"],
"manufacturer": "Freedompro", "manufacturer": "Freedompro",
} }
self._attr_device_class = DEVICE_CLASS_MAP[self._type] self._attr_device_class = DEVICE_CLASS_MAP[device["type"]]
self._attr_state_class = STATE_CLASS_MAP[self._type] self._attr_state_class = STATE_CLASS_MAP[device["type"]]
self._attr_unit_of_measurement = UNIT_MAP[self._type] self._attr_unit_of_measurement = UNIT_MAP[device["type"]]
self._attr_state = 0 self._attr_state = 0
@callback @callback

View File

@ -29,19 +29,16 @@ class Device(CoordinatorEntity, SwitchEntity):
def __init__(self, hass, api_key, device, coordinator): def __init__(self, hass, api_key, device, coordinator):
"""Initialize the Freedompro switch.""" """Initialize the Freedompro switch."""
super().__init__(coordinator) super().__init__(coordinator)
self._hass = hass self._session = aiohttp_client.async_get_clientsession(hass)
self._session = aiohttp_client.async_get_clientsession(self._hass)
self._api_key = api_key self._api_key = api_key
self._attr_name = device["name"] self._attr_name = device["name"]
self._attr_unique_id = device["uid"] self._attr_unique_id = device["uid"]
self._type = device["type"]
self._characteristics = device["characteristics"]
self._attr_device_info = { self._attr_device_info = {
"name": self._attr_name, "name": self.name,
"identifiers": { "identifiers": {
(DOMAIN, self._attr_unique_id), (DOMAIN, self.unique_id),
}, },
"model": self._type, "model": device["type"],
"manufacturer": "Freedompro", "manufacturer": "Freedompro",
} }
self._attr_is_on = False self._attr_is_on = False
@ -53,7 +50,7 @@ class Device(CoordinatorEntity, SwitchEntity):
( (
device device
for device in self.coordinator.data for device in self.coordinator.data
if device["uid"] == self._attr_unique_id if device["uid"] == self.unique_id
), ),
None, None,
) )
@ -75,7 +72,7 @@ class Device(CoordinatorEntity, SwitchEntity):
await put_state( await put_state(
self._session, self._session,
self._api_key, self._api_key,
self._attr_unique_id, self.unique_id,
payload, payload,
) )
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()
@ -87,7 +84,7 @@ class Device(CoordinatorEntity, SwitchEntity):
await put_state( await put_state(
self._session, self._session,
self._api_key, self._api_key,
self._attr_unique_id, self.unique_id,
payload, payload,
) )
await self.coordinator.async_request_refresh() await self.coordinator.async_request_refresh()