Skip to content

Commit

Permalink
Upgraded code to support breaking changes of HA v2012.12.0
Browse files Browse the repository at this point in the history
  • Loading branch information
elad-bar committed Dec 11, 2021
1 parent 2edb668 commit adb9b68
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 42 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
2 changes: 0 additions & 2 deletions custom_components/edgeos/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)


Expand Down
10 changes: 5 additions & 5 deletions custom_components/edgeos/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 *
Expand Down Expand Up @@ -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
Expand All @@ -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}")
Expand Down
31 changes: 16 additions & 15 deletions custom_components/edgeos/clients/web_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
11 changes: 7 additions & 4 deletions custom_components/edgeos/helpers/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions custom_components/edgeos/managers/entity_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand Down Expand Up @@ -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:
Expand All @@ -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(
Expand Down Expand Up @@ -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")
Expand All @@ -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(
Expand All @@ -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")
Expand Down Expand Up @@ -448,6 +446,7 @@ def create_entity(
name: str,
state: int,
attributes: dict,
device_class: Optional[str] = None,
icon: Optional[str] = None,
):
entity = EntityData()
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion custom_components/edgeos/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"codeowners": ["@elad-bar"],
"requirements": ["aiohttp"],
"config_flow": true,
"version": "1.1.5",
"version": "1.1.6",
"iot_class": "local_polling"
}
2 changes: 1 addition & 1 deletion custom_components/edgeos/models/base_entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 9 additions & 1 deletion custom_components/edgeos/models/entity_data.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Optional

from ..helpers.const import *


Expand All @@ -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 = ""
Expand All @@ -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 = {
Expand All @@ -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}"
Expand Down
8 changes: 6 additions & 2 deletions custom_components/edgeos/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion hacs.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "EdgeOS (Ubiquiti)",
"iot_class": "Local Polling"
"iot_class": "Local Polling",
"homeassistant": "2021.12.0"
}
9 changes: 5 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit adb9b68

Please sign in to comment.