diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c32a02..6b5f6e8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.1.6 + +- Upgraded code to support breaking changes of HA v2012.12.0 + ## v1.1.5 - Fixed integration fails to load with EdgeOS version older than 2.0.9 [\#53](https://github.com/elad-bar/ha-edgeos/pull/53) diff --git a/custom_components/edgeos/__init__.py b/custom_components/edgeos/__init__.py index eb29a6d..3809d01 100644 --- a/custom_components/edgeos/__init__.py +++ b/custom_components/edgeos/__init__.py @@ -12,8 +12,6 @@ from .helpers import async_set_ha, clear_ha, get_ha, handle_log_level from .helpers.const import * -REQUIREMENTS = ["aiohttp"] - _LOGGER = logging.getLogger(__name__) diff --git a/custom_components/edgeos/binary_sensor.py b/custom_components/edgeos/binary_sensor.py index e20749c..95036fd 100644 --- a/custom_components/edgeos/binary_sensor.py +++ b/custom_components/edgeos/binary_sensor.py @@ -5,7 +5,7 @@ """ import logging -from homeassistant.const import STATE_OFF, STATE_ON +from homeassistant.components.binary_sensor import BinarySensorEntity from homeassistant.core import HomeAssistant from .helpers.const import * @@ -37,7 +37,7 @@ async def async_unload_entry(hass, config_entry): return True -class EdgeOSBinarySensor(EdgeOSEntity): +class EdgeOSBinarySensor(BinarySensorEntity, EdgeOSEntity): """Representation a binary sensor that is updated by EdgeOS.""" @property @@ -46,9 +46,9 @@ def is_on(self): return bool(self.entity.state) @property - def state(self): - """Return the state of the binary sensor.""" - return STATE_ON if self.is_on else STATE_OFF + def device_class(self) -> BinarySensorDeviceClass: + """Return the class of this sensor.""" + return self.entity.binary_sensor_device_class async def async_added_to_hass_local(self): _LOGGER.info(f"Added new {self.name}") diff --git a/custom_components/edgeos/clients/web_api.py b/custom_components/edgeos/clients/web_api.py index 245239a..2c3bfa5 100644 --- a/custom_components/edgeos/clients/web_api.py +++ b/custom_components/edgeos/clients/web_api.py @@ -165,21 +165,22 @@ async def async_get(self, url): retry_attempt = retry_attempt + 1 try: - async with self._session.get(url, ssl=False) as response: - status = response.status - - message = ( - f"URL: {url}, Status: {response.reason} ({response.status})" - ) - - if status < 400: - result = await response.json() - break - elif status == 403: - self._session = None - self._cookies = {} - - break + if self._session is not None: + async with self._session.get(url, ssl=False) as response: + status = response.status + + message = ( + f"URL: {url}, Status: {response.reason} ({response.status})" + ) + + if status < 400: + result = await response.json() + break + elif status == 403: + self._session = None + self._cookies = {} + + break except Exception as ex: exc_type, exc_obj, tb = sys.exc_info() diff --git a/custom_components/edgeos/helpers/const.py b/custom_components/edgeos/helpers/const.py index 28bc6b8..b686034 100644 --- a/custom_components/edgeos/helpers/const.py +++ b/custom_components/edgeos/helpers/const.py @@ -3,9 +3,12 @@ import voluptuous as vol -from homeassistant.components.binary_sensor import DOMAIN as DOMAIN_BINARY_SENSOR +from homeassistant.components.binary_sensor import ( + DOMAIN as DOMAIN_BINARY_SENSOR, + BinarySensorDeviceClass, +) from homeassistant.components.device_tracker import DOMAIN as DOMAIN_DEVICE_TRACKER -from homeassistant.components.sensor import DOMAIN as DOMAIN_SENSOR +from homeassistant.components.sensor import DOMAIN as DOMAIN_SENSOR, SensorDeviceClass from homeassistant.const import ( ATTR_NAME, ATTR_UNIT_OF_MEASUREMENT, @@ -162,9 +165,7 @@ ATTR_LAST_CHANGED = "Last Changed" ATTR_WEB_SOCKET_LAST_UPDATE = "WS Last Update" ATTR_API_LAST_UPDATE = "API Last Update" -ATTR_DEVICE_CLASS = "device_class" ATTR_UNKNOWN_DEVICES = "Unknown Devices" -DEVICE_CLASS_CONNECTIVITY = "connectivity" DEFAULT_DATE_FORMAT = "%x %X" @@ -239,6 +240,8 @@ ENTITY_DEVICE_NAME = "device-name" ENTITY_UNIQUE_ID = "unique-id" ENTITY_DISABLED = "disabled" +ENTITY_BINARY_SENSOR_DEVICE_CLASS = "binary-sensor-device-class" +ENTITY_SENSOR_DEVICE_CLASS = "sensor-device-class" ENTITY_STATUS = "entity-status" ENTITY_STATUS_EMPTY = None diff --git a/custom_components/edgeos/managers/entity_manager.py b/custom_components/edgeos/managers/entity_manager.py index ba71832..d64c03e 100644 --- a/custom_components/edgeos/managers/entity_manager.py +++ b/custom_components/edgeos/managers/entity_manager.py @@ -282,7 +282,6 @@ def create_binary_sensor( main_entity_details = data.get(main_attribute, FALSE_STR) attributes = { - ATTR_DEVICE_CLASS: DEVICE_CLASS_CONNECTIVITY, ATTR_FRIENDLY_NAME: entity_name, } @@ -318,7 +317,7 @@ def create_binary_sensor( icon = ICONS[sensor_type] self.create_entity( - DOMAIN_BINARY_SENSOR, entity_name, is_on, attributes, icon + DOMAIN_BINARY_SENSOR, entity_name, is_on, attributes, BinarySensorDeviceClass.CONNECTIVITY, icon ) except Exception as ex: @@ -343,7 +342,7 @@ def create_unknown_devices_sensor(self): } self.create_entity( - DOMAIN_SENSOR, entity_name, state, attributes, "mdi:help-rhombus" + DOMAIN_SENSOR, entity_name, state, attributes, None, "mdi:help-rhombus" ) except Exception as ex: self.log_exception( @@ -374,7 +373,7 @@ def create_uptime_sensor( attributes[key] = system_state[key] self.create_entity( - DOMAIN_SENSOR, entity_name, state, attributes, "mdi:timer-sand" + DOMAIN_SENSOR, entity_name, state, attributes, None, "mdi:timer-sand" ) except Exception as ex: self.log_exception(ex, "Failed to create system sensor") @@ -390,7 +389,6 @@ def create_system_status_binary_sensor( if system_state is not None: attributes = { - ATTR_DEVICE_CLASS: DEVICE_CLASS_CONNECTIVITY, ATTR_FRIENDLY_NAME: entity_name, ATTR_API_LAST_UPDATE: api_last_update.strftime(DEFAULT_DATE_FORMAT), ATTR_WEB_SOCKET_LAST_UPDATE: web_socket_last_update.strftime( @@ -407,7 +405,7 @@ def create_system_status_binary_sensor( icon = CONNECTED_ICONS[is_alive] self.create_entity( - DOMAIN_BINARY_SENSOR, entity_name, is_alive, attributes, icon + DOMAIN_BINARY_SENSOR, entity_name, is_alive, attributes, BinarySensorDeviceClass.CONNECTIVITY, icon ) except Exception as ex: self.log_exception(ex, "Failed to create system status binary sensor") @@ -448,6 +446,7 @@ def create_entity( name: str, state: int, attributes: dict, + device_class: Optional[str] = None, icon: Optional[str] = None, ): entity = EntityData() @@ -458,6 +457,11 @@ def create_entity( entity.device_name = DEFAULT_NAME entity.unique_id = f"{DEFAULT_NAME}-{domain}-{name}" + if domain == DOMAIN_BINARY_SENSOR: + entity.binary_sensor_device_class = device_class + elif domain == DOMAIN_SENSOR: + entity.sensor_device_class = device_class + if icon is not None: entity.icon = icon diff --git a/custom_components/edgeos/manifest.json b/custom_components/edgeos/manifest.json index 68317ed..81debba 100644 --- a/custom_components/edgeos/manifest.json +++ b/custom_components/edgeos/manifest.json @@ -6,6 +6,6 @@ "codeowners": ["@elad-bar"], "requirements": ["aiohttp"], "config_flow": true, - "version": "1.1.5", + "version": "1.1.6", "iot_class": "local_polling" } diff --git a/custom_components/edgeos/models/base_entity.py b/custom_components/edgeos/models/base_entity.py index bf42bcc..a03cbed 100644 --- a/custom_components/edgeos/models/base_entity.py +++ b/custom_components/edgeos/models/base_entity.py @@ -95,7 +95,7 @@ def icon(self) -> Optional[str]: return self.entity.icon @property - def device_state_attributes(self): + def extra_state_attributes(self): """Return true if the binary sensor is on.""" return self.entity.attributes diff --git a/custom_components/edgeos/models/entity_data.py b/custom_components/edgeos/models/entity_data.py index 33718fe..7dd56bb 100644 --- a/custom_components/edgeos/models/entity_data.py +++ b/custom_components/edgeos/models/entity_data.py @@ -1,3 +1,5 @@ +from typing import Optional + from ..helpers.const import * @@ -10,6 +12,8 @@ class EntityData: device_name: str status: str disabled: bool + binary_sensor_device_class: Optional[BinarySensorDeviceClass] + sensor_device_class: Optional[SensorDeviceClass] def __init__(self): self.unique_id = "" @@ -20,6 +24,8 @@ def __init__(self): self.device_name = "" self.status = ENTITY_STATUS_CREATED self.disabled = False + self.binary_sensor_device_class: None + self.sensor_device_class: None def __repr__(self): obj = { @@ -29,8 +35,10 @@ def __repr__(self): ENTITY_ICON: self.icon, ENTITY_DEVICE_NAME: self.device_name, ENTITY_STATUS: self.status, - ENTITY_UNIQUE_ID: self.status, + ENTITY_UNIQUE_ID: self.unique_id, ENTITY_DISABLED: self.disabled, + ENTITY_BINARY_SENSOR_DEVICE_CLASS: self.binary_sensor_device_class, + ENTITY_SENSOR_DEVICE_CLASS: self.sensor_device_class } to_string = f"{obj}" diff --git a/custom_components/edgeos/sensor.py b/custom_components/edgeos/sensor.py index 544d8a8..97e54cc 100644 --- a/custom_components/edgeos/sensor.py +++ b/custom_components/edgeos/sensor.py @@ -4,7 +4,6 @@ https://home-assistant.io/components/binary_sensor.edgeos/ """ import logging -from typing import Union from custom_components.edgeos.helpers.const import * from custom_components.edgeos.models.base_entity import ( @@ -43,13 +42,18 @@ class EdgeOSSensor(EdgeOSEntity): """Representation a binary sensor that is updated by EdgeOS.""" @property - def state(self) -> Union[None, str, int, float]: + def native_value(self): """Return the state of the sensor.""" return self.entity.state async def async_added_to_hass_local(self): _LOGGER.info(f"Added new {self.name}") + @property + def device_class(self) -> SensorDeviceClass: + """Return the class of this sensor.""" + return self.entity.sensor_device_class + def _immediate_update(self, previous_state: bool): if previous_state != self.entity.state: _LOGGER.debug( diff --git a/hacs.json b/hacs.json index c7db4c9..d1afd2b 100644 --- a/hacs.json +++ b/hacs.json @@ -1,4 +1,5 @@ { "name": "EdgeOS (Ubiquiti)", - "iot_class": "Local Polling" + "iot_class": "Local Polling", + "homeassistant": "2021.12.0" } diff --git a/requirements.txt b/requirements.txt index 122dcd6..cfc8a89 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,5 @@ -aiohttp~=3.7.4.post0 -homeassistant~=2021.11.0 -cryptography~=3.3.2 -voluptuous~=0.12.1 +pre-commit +homeassistant==2021.12.0b6 +voluptuous +aiohttp +cryptography