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

Commit

Permalink
fixes for #8 and #9
Browse files Browse the repository at this point in the history
  • Loading branch information
eavanvalkenburg committed Jan 7, 2020
1 parent 73db176 commit 413eb27
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 30 deletions.
7 changes: 4 additions & 3 deletions custom_components/sia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def setup(hass, config):
for component in ["binary_sensor", "alarm_control_panel", "sensor"]:
discovery.load_platform(hass, component, DOMAIN, {}, config)

# for hub in HASS_PLATFORM.data[DOMAIN].values():
# for sensor in hub._states.values():
# sensor.async_schedule_update_ha_state()
for hub in HASS_PLATFORM.data[DOMAIN].values():
for sensor in hub._states.values():
sensor.async_schedule_update_ha_state()

server = socketserver.TCPServer(("", port), AlarmTCPHandler)

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
28 changes: 18 additions & 10 deletions custom_components/sia/alarm_control_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,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 @@ -93,10 +97,9 @@ async def async_added_to_hass(self):
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 @@ -116,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 @@ -184,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
27 changes: 18 additions & 9 deletions custom_components/sia/sensor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
"""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, DATA_UPDATED
Expand All @@ -25,15 +28,18 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async_add_entities(devices)


class SIASensor(Entity, RestoreEntity):
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
Expand All @@ -45,24 +51,27 @@ async def async_added_to_hass(self):
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 = 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))
self._async_track_unavailable()
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):
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 413eb27

Please sign in to comment.