Skip to content
This repository has been archived by the owner on Jul 7, 2022. It is now read-only.

Commit

Permalink
Merge pull request #11 from eavanvalkenburg/beta
Browse files Browse the repository at this point in the history
Fixes for #8 and #9
  • Loading branch information
eavanvalkenburg authored Jan 7, 2020
2 parents b491889 + 413eb27 commit 7828cb3
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 32 deletions.
3 changes: 2 additions & 1 deletion custom_components/sia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
_LOGGER = logging.getLogger(__name__)

DOMAIN = "sia"

DATA_UPDATED = f"{DOMAIN}_data_updated"
CONF_ACCOUNT = "account"
CONF_ENCRYPTION_KEY = "encryption_key"
CONF_HUBS = "hubs"
Expand Down Expand Up @@ -255,6 +255,7 @@ def _upsert_sensor(self, zone, sensor_type):
)
if constructor and sensor_name:
new_sensor = eval(constructor)(
self._name,
sensor_id,
sensor_name,
sensor_type,
Expand Down
40 changes: 26 additions & 14 deletions custom_components/sia/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from homeassistant.core import callback
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.components.alarm_control_panel import AlarmControlPanel
Expand All @@ -19,6 +20,7 @@
ALARM_FORMAT,
CONF_PING_INTERVAL,
CONF_ZONE,
DATA_UPDATED,
PING_INTERVAL_MARGIN,
STATE_ALARM_ARMED_AWAY,
STATE_ALARM_ARMED_CUSTOM_BYPASS,
Expand Down Expand Up @@ -46,25 +48,29 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class SIAAlarmControlPanel(AlarmControlPanel, RestoreEntity):
"""Class for SIA Alarm Control Panels."""

def __init__(self, entity_id, name, device_class, zone, ping_interval, hass):
def __init__(
self, hub_name, entity_id, name, device_class, zone, ping_interval, hass
):
_LOGGER.debug(
"SIAAlarmControlPanel: init: Initializing SIA Alarm Control Panel: "
+ entity_id
)
self._should_poll = False
self._entity_id = generate_entity_id(
self.entity_id = generate_entity_id(
entity_id_format=ALARM_FORMAT, name=entity_id, hass=hass
)
self._unique_id = f"{hub_name}-{self.entity_id}"
self._name = name
self.hass = hass
self._ping_interval = ping_interval
self._attr = {CONF_PING_INTERVAL: self.ping_interval, CONF_ZONE: zone}
self._is_available = True
self._remove_unavailability_tracker = None
self._state = STATE_ALARM_DISARMED
self._state = None

async def async_added_to_hass(self):
"""Once the panel is added, see if it was there before and pull in that state."""
_LOGGER.debug("SIAAlarmControlPanel: init: added_to_hass")
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state is not None and state.state is not None:
Expand All @@ -82,17 +88,18 @@ async def async_added_to_hass(self):
else:
self.state = None
else:
self.state = STATE_ALARM_DISARMED # assume disarmed
_LOGGER.debug("SIAAlarmControlPanel: no previous state.")
return
# self.state = STATE_ALARM_DISARMED # assume disarmed
_LOGGER.debug("SIAAlarmControlPanel: added: state: " + str(state))
self._async_track_unavailable()
# async_dispatcher_connect(
# self._hass, DATA_UPDATED, self._schedule_immediate_update
# )
async_dispatcher_connect(
self.hass, DATA_UPDATED, self._schedule_immediate_update
)

@property
def entity_id(self):
"""Get entity_id."""
return self._entity_id
@callback
def _schedule_immediate_update(self):
self.async_schedule_update_ha_state(True)

@property
def name(self):
Expand All @@ -112,7 +119,7 @@ def state(self):
@property
def unique_id(self) -> str:
"""Get unique_id."""
return self._name
return self._unique_id

@property
def available(self):
Expand Down Expand Up @@ -180,5 +187,10 @@ def _async_set_unavailable(self, now):
@property
def supported_features(self) -> int:
"""Return the list of supported features."""
return SUPPORT_ALARM_ARM_AWAY | SUPPORT_ALARM_ARM_CUSTOM_BYPASS | SUPPORT_ALARM_ARM_HOME | SUPPORT_ALARM_ARM_NIGHT | SUPPORT_ALARM_TRIGGER

return (
SUPPORT_ALARM_ARM_AWAY
| SUPPORT_ALARM_ARM_CUSTOM_BYPASS
| SUPPORT_ALARM_ARM_HOME
| SUPPORT_ALARM_ARM_NIGHT
| SUPPORT_ALARM_TRIGGER
)
23 changes: 15 additions & 8 deletions custom_components/sia/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from homeassistant.core import callback
from homeassistant.helpers.entity import generate_entity_id
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.event import async_track_point_in_utc_time
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util.dt import utcnow
Expand All @@ -12,6 +13,7 @@
CONF_PING_INTERVAL,
PING_INTERVAL_MARGIN,
CONF_ZONE,
DATA_UPDATED,
BINARY_SENSOR_FORMAT,
STATE_ON,
STATE_OFF,
Expand All @@ -36,25 +38,23 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class SIABinarySensor(RestoreEntity):
"""Class for SIA Binary Sensors."""

def __init__(self, entity_id, name, device_class, zone, ping_interval, hass):
def __init__(
self, hub_name, entity_id, name, device_class, zone, ping_interval, hass
):
self._device_class = device_class
self._should_poll = False
self._ping_interval = ping_interval
self._attr = {CONF_PING_INTERVAL: self.ping_interval, CONF_ZONE: zone}
self._entity_id = generate_entity_id(
self.entity_id = generate_entity_id(
entity_id_format=BINARY_SENSOR_FORMAT, name=entity_id, hass=hass
)
self._unique_id = f"{hub_name}-{self.entity_id}"
self._name = name
self.hass = hass
self._is_available = True
self._remove_unavailability_tracker = None
self._state = None

@property
def entity_id(self):
"""Get entity_id."""
return self._entity_id

async def async_added_to_hass(self):
await super().async_added_to_hass()
state = await self.async_get_last_state()
Expand All @@ -64,6 +64,13 @@ async def async_added_to_hass(self):
self.state = None
_LOGGER.debug("SIABinarySensor: added: state: " + str(state))
self._async_track_unavailable()
async_dispatcher_connect(
self.hass, DATA_UPDATED, self._schedule_immediate_update
)

@callback
def _schedule_immediate_update(self):
self.async_schedule_update_ha_state(True)

@property
def name(self):
Expand All @@ -80,7 +87,7 @@ def state(self):

@property
def unique_id(self) -> str:
return self._name
return self._unique_id

@property
def available(self):
Expand Down
42 changes: 33 additions & 9 deletions custom_components/sia/sensor.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
"""Module for SIA Sensors."""

import logging
import datetime as dt

from homeassistant.core import callback
from homeassistant.components.sensor import ENTITY_ID_FORMAT as SENSOR_FORMAT

from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity import Entity, generate_entity_id
from homeassistant.helpers.restore_state import RestoreEntity
from homeassistant.util.dt import utcnow

from . import CONF_ZONE, CONF_PING_INTERVAL
from . import CONF_ZONE, CONF_PING_INTERVAL, DATA_UPDATED

DOMAIN = "sia"
_LOGGER = logging.getLogger(__name__)
Expand All @@ -25,29 +28,50 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async_add_entities(devices)


class SIASensor(Entity):
class SIASensor(RestoreEntity):
"""Class for SIA Sensors."""

def __init__(self, entity_id, name, device_class, zone, ping_interval, hass):
def __init__(
self, hub_name, entity_id, name, device_class, zone, ping_interval, hass
):
self._should_poll = False
self._device_class = device_class
self._entity_id = generate_entity_id(
self.entity_id = generate_entity_id(
entity_id_format=SENSOR_FORMAT, name=entity_id, hass=hass
)
self._unique_id = f"{hub_name}-{self.entity_id}"
self._state = utcnow()
self._attr = {CONF_PING_INTERVAL: str(ping_interval), CONF_ZONE: zone}
self._name = name
self.hass = hass

@property
def entity_id(self):
"""Get entity_id."""
return self._entity_id
async def async_added_to_hass(self):
"""Once the sensor is added, see if it was there before and pull in that state."""
await super().async_added_to_hass()
state = await self.async_get_last_state()
if state is not None and state.state is not None:
_LOGGER.debug("SIASensor: init: old state: " + state.state)
self.state = dt.datetime.strptime(state.state, "%Y-%m-%dT%H:%M:%S.%f%z")
else:
return
_LOGGER.debug("SIASensor: added: state: " + str(state))
async_dispatcher_connect(
self.hass, DATA_UPDATED, self._schedule_immediate_update
)

@callback
def _schedule_immediate_update(self):
self.async_schedule_update_ha_state(True)

@property
def name(self):
return self._name

@property
def unique_id(self) -> str:
"""Get unique_id."""
return self._unique_id

@property
def state(self):
return self._state.isoformat()
Expand Down

0 comments on commit 7828cb3

Please sign in to comment.