Skip to content

Commit

Permalink
Event entity for login with wrong code
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Aug 2, 2023
1 parent b60e298 commit 5e1736b
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 2 deletions.
1 change: 1 addition & 0 deletions custom_components/jablotron100/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
PLATFORMS: Final = [
Platform.ALARM_CONTROL_PANEL,
Platform.BINARY_SENSOR,
Platform.EVENT,
Platform.SENSOR,
Platform.SWITCH
];
Expand Down
4 changes: 4 additions & 0 deletions custom_components/jablotron100/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ class EntityType(StrEnum):
DEVICE_STATE_BUTTON = "device_state_button"
DEVICE_STATE_VALVE = "device_state_valve"
DEVICE_STATE_CUSTOM = "device_state_custom"
EVENT_LOGIN = "event_login"
FIRE = "fire"
GSM_SIGNAL = "gsm_signal"
GSM_SIGNAL_STRENGTH = "gsm_signal_strength"
Expand All @@ -126,6 +127,9 @@ class EntityType(StrEnum):
SIGNAL_STRENGTH = "signal_strength"
TEMPERATURE = "temperature"

class EventLoginType(StrEnum):
WRONG_CODE = "wrong_code"

class PartiallyArmingMode(StrEnum):
NOT_SUPPORTED = "not_supported"
NIGHT_MODE = "night_mode"
Expand Down
63 changes: 63 additions & 0 deletions custom_components/jablotron100/event.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
from __future__ import annotations
from homeassistant.components.event import (
EventEntity,
EventEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import callback, HomeAssistant
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddEntitiesCallback

from typing import Dict
from .const import (
DATA_JABLOTRON,
DOMAIN,
EntityType,
EventLoginType,
)
from .jablotron import Jablotron, JablotronControl, JablotronEntity

EVENT_TYPES: Dict[EntityType, EventEntityDescription] = {
EntityType.EVENT_LOGIN: EventEntityDescription(
key=EntityType.EVENT_LOGIN,
translation_key="login",
icon="mdi:login",
event_types=[EventLoginType.WRONG_CODE.value],
),
}

async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry, async_add_entities: AddEntitiesCallback) -> None:
jablotron_instance: Jablotron = hass.data[DOMAIN][config_entry.entry_id][DATA_JABLOTRON]

@callback
def add_entities() -> None:
entities = []

for entity_type in EVENT_TYPES:
for entity in jablotron_instance.entities[entity_type].values():
if entity.id not in jablotron_instance.hass_entities:
entities.append(JablotronEventEntity(jablotron_instance, entity, EVENT_TYPES[entity_type]))

async_add_entities(entities)

add_entities()

config_entry.async_on_unload(
async_dispatcher_connect(hass, jablotron_instance.signal_entities_added(), add_entities)
)

class JablotronEventEntity(JablotronEntity, EventEntity):

def __init__(
self,
jablotron: Jablotron,
control: JablotronControl,
description: EventEntityDescription,
) -> None:
self.entity_description = description

super().__init__(jablotron, control)

def trigger_event(self, event: EventLoginType) -> None:
self._trigger_event(event.value)
self.async_write_ha_state()
12 changes: 12 additions & 0 deletions custom_components/jablotron100/jablotron.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
EVENT_WRONG_CODE,
EMPTY_PACKET,
EntityType,
EventLoginType,
HIDRAW_PATH,
LOGGER,
MAX_SECTIONS,
Expand Down Expand Up @@ -822,6 +823,13 @@ def _create_central_unit_sensors(self) -> None:
if CentralUnitData.BUSES not in self._central_unit_data:
self._central_unit_data[CentralUnitData.BUSES] = [1]

self._add_entity(
None,
EntityType.EVENT_LOGIN,
"login",
STATE_ON, # Fake state so the entity is available
)

self._add_entity(
None,
EntityType.POWER_SUPPLY,
Expand Down Expand Up @@ -946,6 +954,10 @@ def _has_pg_outputs(self) -> bool:
return self._config[CONF_NUMBER_OF_PG_OUTPUTS] > 0

def _login_error(self) -> None:
for control in self.entities[EntityType.EVENT_LOGIN].values():
if control.id in self.hass_entities:
self.hass_entities[control.id].trigger_event(EventLoginType.WRONG_CODE)

self._hass.bus.fire(EVENT_WRONG_CODE)

def _read_packets(self) -> None:
Expand Down
12 changes: 12 additions & 0 deletions custom_components/jablotron100/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@
"name": "Problem"
}
},
"event": {
"login": {
"name": "Login",
"state_attributes": {
"event_type": {
"state": {
"wrong_code": "Wrong code"
}
}
}
}
},
"sensor": {
"battery_level": {
"name": "Battery level"
Expand Down
12 changes: 12 additions & 0 deletions custom_components/jablotron100/translations/cs.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@
"name": "Problém"
}
},
"event": {
"login": {
"name": "Přihlášení",
"state_attributes": {
"event_type": {
"state": {
"wrong_code": "Nesprávný kód"
}
}
}
}
},
"sensor": {
"battery_level": {
"name": "Úroveň baterie"
Expand Down
12 changes: 12 additions & 0 deletions custom_components/jablotron100/translations/da.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@
"name": "Problem"
}
},
"event": {
"login": {
"name": "Login",
"state_attributes": {
"event_type": {
"state": {
"wrong_code": "Wrong code"
}
}
}
}
},
"sensor": {
"battery_level": {
"name": "Batteri niveau"
Expand Down
12 changes: 12 additions & 0 deletions custom_components/jablotron100/translations/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@
"name": "Problem"
}
},
"event": {
"login": {
"name": "Login",
"state_attributes": {
"event_type": {
"state": {
"wrong_code": "Wrong code"
}
}
}
}
},
"sensor": {
"battery_level": {
"name": "Battery level"
Expand Down
12 changes: 12 additions & 0 deletions custom_components/jablotron100/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@
"name": "Problem"
}
},
"event": {
"login": {
"name": "Login",
"state_attributes": {
"event_type": {
"state": {
"wrong_code": "Wrong code"
}
}
}
}
},
"sensor": {
"battery_level": {
"name": "Battery level"
Expand Down
12 changes: 12 additions & 0 deletions custom_components/jablotron100/translations/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@
"name": "Problem"
}
},
"event": {
"login": {
"name": "Login",
"state_attributes": {
"event_type": {
"state": {
"wrong_code": "Wrong code"
}
}
}
}
},
"sensor": {
"battery_level": {
"name": "Battery level"
Expand Down
12 changes: 12 additions & 0 deletions custom_components/jablotron100/translations/nb.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@
"name": "Problem"
}
},
"event": {
"login": {
"name": "Login",
"state_attributes": {
"event_type": {
"state": {
"wrong_code": "Wrong code"
}
}
}
}
},
"sensor": {
"battery_level": {
"name": "Battery level"
Expand Down
12 changes: 12 additions & 0 deletions custom_components/jablotron100/translations/nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@
"name": "Problem"
}
},
"event": {
"login": {
"name": "Login",
"state_attributes": {
"event_type": {
"state": {
"wrong_code": "Wrong code"
}
}
}
}
},
"sensor": {
"battery_level": {
"name": "Battery level"
Expand Down
12 changes: 12 additions & 0 deletions custom_components/jablotron100/translations/sk.json
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,18 @@
"name": "Problem"
}
},
"event": {
"login": {
"name": "Login",
"state_attributes": {
"event_type": {
"state": {
"wrong_code": "Wrong code"
}
}
}
}
},
"sensor": {
"battery_level": {
"name": "Battery level"
Expand Down
3 changes: 1 addition & 2 deletions hacs.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
{
"name": "Jablotron 100",
"content_in_root": false,
"domains": ["alarm_control_panel", "binary_sensor", "sensor", "switch"],
"domains": ["alarm_control_panel", "binary_sensor", "event", "sensor", "switch"],
"country": ["CS", "DA", "DE", "EN", "IT", "NB", "NL", "SK"],
"homeassistant": "2023.8.0",
"render_readme": true
}

0 comments on commit 5e1736b

Please sign in to comment.