|
| 1 | +"""MyGekko Cams implementation""" |
| 2 | +from __future__ import annotations |
| 3 | + |
| 4 | +from enum import IntEnum |
| 5 | + |
| 6 | +from PyMyGekko.data_provider import DataProviderBase |
| 7 | +from PyMyGekko.data_provider import EntityValueAccessor |
| 8 | +from PyMyGekko.resources import Entity |
| 9 | + |
| 10 | + |
| 11 | +class Cam(Entity): |
| 12 | + """Class for MyGekko Cam""" |
| 13 | + |
| 14 | + def __init__( |
| 15 | + self, entity_id: str, name: str, value_accessor: CamValueAccessor |
| 16 | + ) -> None: |
| 17 | + super().__init__(entity_id, name, "/cams/") |
| 18 | + self._value_accessor = value_accessor |
| 19 | + self._supported_features = self._value_accessor.get_features(self) |
| 20 | + |
| 21 | + @property |
| 22 | + def supported_features(self) -> list[CamFeature]: |
| 23 | + """Returns the supported features""" |
| 24 | + return self._supported_features |
| 25 | + |
| 26 | + @property |
| 27 | + def image_url(self) -> str | None: |
| 28 | + """Returns the image url""" |
| 29 | + return self._value_accessor.get_value(self, "imagepath") |
| 30 | + |
| 31 | + @property |
| 32 | + def stream_url(self) -> str | None: |
| 33 | + """Returns the stream url""" |
| 34 | + return self._value_accessor.get_value(self, "streampath") |
| 35 | + |
| 36 | + |
| 37 | +class CamNewRecordAvailableState(IntEnum): |
| 38 | + """MyGekko Cams Record Available State""" |
| 39 | + |
| 40 | + NO = 0 |
| 41 | + YES = 1 |
| 42 | + |
| 43 | + |
| 44 | +class CamFeature(IntEnum): |
| 45 | + """MyGekko Cams Feature""" |
| 46 | + |
| 47 | + ON_OFF = 0 |
| 48 | + STREAM = 1 |
| 49 | + |
| 50 | + |
| 51 | +class CamValueAccessor(EntityValueAccessor): |
| 52 | + """Cam value accessor""" |
| 53 | + |
| 54 | + def __init__(self, data_provider: DataProviderBase): |
| 55 | + self._data = {} |
| 56 | + self._data_provider = data_provider |
| 57 | + self._data_provider.subscribe(self) |
| 58 | + |
| 59 | + def update_status(self, status, hardware): |
| 60 | + if status is not None and "cams" in status: |
| 61 | + cams = status["cams"] |
| 62 | + for key in cams: |
| 63 | + if key.startswith("item"): |
| 64 | + if key not in self._data: |
| 65 | + self._data[key] = {} |
| 66 | + |
| 67 | + if "sumstate" in cams[key] and "value" in cams[key]["sumstate"]: |
| 68 | + ( |
| 69 | + self._data[key]["newRecordsAvailableState"], |
| 70 | + *_other, |
| 71 | + ) = cams[ |
| 72 | + key |
| 73 | + ]["sumstate"]["value"].split( |
| 74 | + ";", |
| 75 | + ) |
| 76 | + |
| 77 | + def update_resources(self, resources): |
| 78 | + if resources is not None and "cams" in resources: |
| 79 | + cams = resources["cams"] |
| 80 | + for key in cams: |
| 81 | + if key.startswith("item"): |
| 82 | + if key not in self._data: |
| 83 | + self._data[key] = {} |
| 84 | + self._data[key]["name"] = cams[key]["name"] |
| 85 | + self._data[key]["imagepath"] = cams[key]["imagepath"] |
| 86 | + self._data[key]["streampath"] = cams[key]["streampath"] |
| 87 | + |
| 88 | + @property |
| 89 | + def cams(self): |
| 90 | + """Returns the cams read from MyGekko""" |
| 91 | + result: list[Cam] = [] |
| 92 | + for key, data in self._data.items(): |
| 93 | + result.append(Cam(key, data["name"], self)) |
| 94 | + |
| 95 | + return result |
| 96 | + |
| 97 | + def get_features(self, door: Cam) -> list[CamFeature]: |
| 98 | + """Returns the supported features""" |
| 99 | + result = list() |
| 100 | + |
| 101 | + if door and door.entity_id: |
| 102 | + if door.entity_id in self._data: |
| 103 | + data = self._data[door.entity_id] |
| 104 | + if "streampath" in data and data["streampath"]: |
| 105 | + result.append(CamFeature.STREAM) |
| 106 | + |
| 107 | + return result |
0 commit comments