|
| 1 | +"""MyGekko DoorInterComs implementation""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from datetime import datetime |
| 5 | +from enum import IntEnum, StrEnum |
| 6 | + |
| 7 | +from PyMyGekko.data_provider import DataProviderBase |
| 8 | +from PyMyGekko.data_provider import EntityValueAccessor |
| 9 | +from PyMyGekko.resources import Entity |
| 10 | + |
| 11 | + |
| 12 | +class DoorInterCom(Entity): |
| 13 | + """Class for MyGekko DoorInterCom""" |
| 14 | + |
| 15 | + def __init__( |
| 16 | + self, entity_id: str, name: str, value_accessor: DoorInterComValueAccessor |
| 17 | + ) -> None: |
| 18 | + super().__init__(entity_id, name, "/door_intercom/") |
| 19 | + self._value_accessor = value_accessor |
| 20 | + self._supported_features = self._value_accessor.get_features(self) |
| 21 | + |
| 22 | + @property |
| 23 | + def supported_features(self) -> list[DoorInterComFeature]: |
| 24 | + """Returns the supported features""" |
| 25 | + return self._supported_features |
| 26 | + |
| 27 | + @property |
| 28 | + def image_url(self) -> str | None: |
| 29 | + """Returns the image url""" |
| 30 | + return self._value_accessor.get_value(self, "imagepath") |
| 31 | + |
| 32 | + @property |
| 33 | + def stream_url(self) -> str | None: |
| 34 | + """Returns the stream url""" |
| 35 | + return self._value_accessor.get_value(self, "streampath") |
| 36 | + |
| 37 | + @property |
| 38 | + def sound_mode(self) -> DoorInterComSoundMode | None: |
| 39 | + """Returns the sound mode""" |
| 40 | + value = self._value_accessor.get_value(self, "soundMode") |
| 41 | + return DoorInterComSoundMode(int(value)) if value is not None else None |
| 42 | + |
| 43 | + @property |
| 44 | + def action_on_ring_state(self) -> DoorInterComActionOnRingState | None: |
| 45 | + """Returns the action on ring state""" |
| 46 | + value = self._value_accessor.get_value(self, "actionOnRingState") |
| 47 | + return DoorInterComActionOnRingState(int(value)) if value is not None else None |
| 48 | + |
| 49 | + @property |
| 50 | + def connection_state(self) -> DoorInterComConnectionState | None: |
| 51 | + """Returns the connection state""" |
| 52 | + value = self._value_accessor.get_value(self, "connectionState") |
| 53 | + return DoorInterComConnectionState(int(value)) if value is not None else None |
| 54 | + |
| 55 | + @property |
| 56 | + def missed_calls(self) -> int | None: |
| 57 | + """Returns the missed calls""" |
| 58 | + value = self._value_accessor.get_value(self, "missedCallsValue") |
| 59 | + return int(value) if value else None |
| 60 | + |
| 61 | + @property |
| 62 | + def last_missed_call_date(self) -> datetime | None: |
| 63 | + """Returns the missed calls""" |
| 64 | + value = self._value_accessor.get_value(self, "lastMissedCallDate") |
| 65 | + return datetime.strptime(value, "%d.%m.%Y %H:%M:%S") if value else None |
| 66 | + |
| 67 | + |
| 68 | +class DoorInterComFeature(IntEnum): |
| 69 | + """MyGekko Door Inter Com Feature""" |
| 70 | + |
| 71 | + ON_OFF = 0 |
| 72 | + STREAM = 1 |
| 73 | + |
| 74 | + |
| 75 | +class DoorInterComSoundMode(IntEnum): |
| 76 | + """MyGekko DoorInterCom Sound Mode""" |
| 77 | + |
| 78 | + MUTE = 0 |
| 79 | + RINGING = 1 |
| 80 | + |
| 81 | + |
| 82 | +class DoorInterComActionOnRingState(IntEnum): |
| 83 | + """MyGekko DoorInterCom Action on ring state""" |
| 84 | + |
| 85 | + OFF = 0 |
| 86 | + ON = 1 |
| 87 | + |
| 88 | + |
| 89 | +class DoorInterComConnectionState(IntEnum): |
| 90 | + """MyGekko DoorInterCom Connection state""" |
| 91 | + |
| 92 | + ERROR_PROCESSING = -6 |
| 93 | + ERROR_AUTHORIZATION = -5 |
| 94 | + VOIP_NOT_ACTIVE = -4 |
| 95 | + ERROR_FAV_CHECK = -3 |
| 96 | + ERROR_PROVISIONING = -2 |
| 97 | + ERROR_CONNECTION = -1 |
| 98 | + NOT_SET_UP = (0,) |
| 99 | + OK = 1 |
| 100 | + |
| 101 | + |
| 102 | +class DoorInterComCommand(StrEnum): |
| 103 | + """MyGekko DoorInterCom Commands""" |
| 104 | + |
| 105 | + OPEN = "O" |
| 106 | + RINGING = "M1" |
| 107 | + MUTE = "M0" |
| 108 | + ACTION_ON = "A1" |
| 109 | + ACTION_OFF = "A0" |
| 110 | + |
| 111 | + |
| 112 | +class DoorInterComValueAccessor(EntityValueAccessor): |
| 113 | + """DoorInterCom value accessor""" |
| 114 | + |
| 115 | + def __init__(self, data_provider: DataProviderBase): |
| 116 | + self._data = {} |
| 117 | + self._data_provider = data_provider |
| 118 | + self._data_provider.subscribe(self) |
| 119 | + |
| 120 | + def update_status(self, status, hardware): |
| 121 | + if status is not None and "door_intercom" in status: |
| 122 | + door_inter_coms = status["door_intercom"] |
| 123 | + for key in door_inter_coms: |
| 124 | + if key.startswith("item"): |
| 125 | + if key not in self._data: |
| 126 | + self._data[key] = {} |
| 127 | + |
| 128 | + if ( |
| 129 | + "sumstate" in door_inter_coms[key] |
| 130 | + and "value" in door_inter_coms[key]["sumstate"] |
| 131 | + ): |
| 132 | + ( |
| 133 | + self._data[key]["soundMode"], |
| 134 | + self._data[key]["actionOnRingState"], |
| 135 | + self._data[key]["connectionState"], |
| 136 | + self._data[key]["missedCallsValue"], |
| 137 | + self._data[key]["lastMissedCallDate"], |
| 138 | + *_other, |
| 139 | + ) = door_inter_coms[key]["sumstate"]["value"].split( |
| 140 | + ";", |
| 141 | + ) |
| 142 | + |
| 143 | + def update_resources(self, resources): |
| 144 | + if resources is not None and "door_intercom" in resources: |
| 145 | + door_inter_coms = resources["door_intercom"] |
| 146 | + for key in door_inter_coms: |
| 147 | + if key.startswith("item"): |
| 148 | + if key not in self._data: |
| 149 | + self._data[key] = {} |
| 150 | + self._data[key]["name"] = door_inter_coms[key]["name"] |
| 151 | + self._data[key]["imagepath"] = door_inter_coms[key].get( |
| 152 | + "imagepath", None |
| 153 | + ) |
| 154 | + self._data[key]["streampath"] = door_inter_coms[key].get( |
| 155 | + "streampath", None |
| 156 | + ) |
| 157 | + |
| 158 | + @property |
| 159 | + def door_inter_coms(self): |
| 160 | + """Returns the door intercoms read from MyGekko""" |
| 161 | + result: list[DoorInterCom] = [] |
| 162 | + for key, data in self._data.items(): |
| 163 | + result.append(DoorInterCom(key, data["name"], self)) |
| 164 | + |
| 165 | + return result |
| 166 | + |
| 167 | + def get_features(self, door_inter_com: DoorInterCom) -> list[DoorInterComFeature]: |
| 168 | + """Returns the supported features""" |
| 169 | + result = list() |
| 170 | + |
| 171 | + if door_inter_com and door_inter_com.entity_id: |
| 172 | + if door_inter_com.entity_id in self._data: |
| 173 | + data = self._data[door_inter_com.entity_id] |
| 174 | + if "streampath" in data and data["streampath"]: |
| 175 | + result.append(DoorInterComFeature.STREAM) |
| 176 | + |
| 177 | + return result |
| 178 | + |
| 179 | + async def send_command( |
| 180 | + self, door_inter_com: DoorInterCom, state: DoorInterComCommand |
| 181 | + ) -> None: |
| 182 | + """Sends the command""" |
| 183 | + if door_inter_com and door_inter_com.entity_id: |
| 184 | + await self._data_provider.write_data( |
| 185 | + door_inter_com.resource_path, str(state) |
| 186 | + ) |
0 commit comments