1
1
"""
2
2
Camera
3
- Version: v2024.09.0
3
+ Version: v2024.10.0b2
4
4
"""
5
5
6
6
from __future__ import annotations
20
20
from PIL import Image
21
21
from homeassistant import config_entries , core
22
22
from homeassistant .components .camera import PLATFORM_SCHEMA , Camera , CameraEntityFeature
23
+ from homeassistant .helpers .update_coordinator import CoordinatorEntity
23
24
from homeassistant .const import CONF_NAME , CONF_UNIQUE_ID , MATCH_ALL
24
25
from homeassistant .helpers import config_validation as cv
25
- from homeassistant .helpers .entity_platform import AddEntitiesCallback
26
- from homeassistant .helpers .reload import async_setup_reload_service
27
26
from homeassistant .helpers .storage import STORAGE_DIR
28
- from homeassistant .helpers .typing import ConfigType , DiscoveryInfoType
29
27
from psutil_home_assistant import PsutilWrapper as ProcInsp
30
28
import voluptuous as vol
31
29
32
- from .camera_processing import CameraProcessor
33
- from .camera_shared import CameraSharedManager
34
30
from .common import get_vacuum_unique_id_from_mqtt_topic
35
31
from .const import (
36
32
ATTR_FRIENDLY_NAME ,
45
41
DEFAULT_NAME ,
46
42
DOMAIN ,
47
43
NOT_STREAMING_STATES ,
48
- PLATFORMS ,
49
44
)
50
- from .snapshots .snapshot import Snapshots
51
45
from .types import SnapshotStore
52
46
from .utils .colors_man import ColorsManagment
53
47
from .utils .files_operations import async_get_active_user_language , is_auth_updated
54
- from .valetudo .MQTT .connector import ValetudoConnector
48
+ from .snapshots .snapshot import Snapshots
49
+ from .camera_processing import CameraProcessor
55
50
56
51
PLATFORM_SCHEMA = PLATFORM_SCHEMA .extend (
57
52
{
@@ -73,27 +68,16 @@ async def async_setup_entry(
73
68
) -> None :
74
69
"""Setup camera from a config entry created in the integrations UI."""
75
70
config = hass .data [DOMAIN ][config_entry .entry_id ]
71
+ coordinator = hass .data [DOMAIN ][config_entry .entry_id ]["coordinator" ]
76
72
# Update our config to and eventually add or remove option.
77
73
if config_entry .options :
78
74
config .update (config_entry .options )
79
75
80
- camera = [ValetudoCamera ( hass , config )]
76
+ camera = [MQTTCamera ( coordinator , config )]
81
77
async_add_entities (camera , update_before_add = True )
82
78
83
79
84
- async def async_setup_platform (
85
- hass : core .HomeAssistant ,
86
- config : ConfigType ,
87
- async_add_entities : AddEntitiesCallback ,
88
- discovery_info : DiscoveryInfoType | None = None ,
89
- ):
90
- """Set up the camera platform."""
91
- async_add_entities ([ValetudoCamera (hass , config )])
92
-
93
- await async_setup_reload_service (hass , DOMAIN , PLATFORMS )
94
-
95
-
96
- class ValetudoCamera (Camera ):
80
+ class MQTTCamera (CoordinatorEntity , Camera ):
97
81
"""
98
82
Rend the vacuum map and the vacuum state for:
99
83
Valetudo Hypfer and rand256 Firmwares Vacuums maps.
@@ -103,27 +87,25 @@ class ValetudoCamera(Camera):
103
87
_attr_has_entity_name = True
104
88
_unrecorded_attributes = frozenset ({MATCH_ALL })
105
89
106
- def __init__ (self , hass , device_info ):
107
- super ().__init__ ()
108
- self .hass = hass
90
+ def __init__ (self , coordinator , device_info ):
91
+ super ().__init__ (coordinator )
92
+ Camera .__init__ (self )
93
+ self .hass = coordinator .hass
109
94
self ._state = "init"
110
95
self ._attr_model = "MQTT Vacuums"
111
96
self ._attr_brand = "MQTT Vacuum Camera"
112
97
self ._attr_name = "Camera"
113
98
self ._attr_is_on = True
114
99
self ._directory_path = self .hass .config .path () # get Home Assistant path
115
- self ._mqtt_listen_topic = str (device_info .get (CONF_VACUUM_CONNECTION_STRING ))
116
- self ._shared , self ._file_name = self ._handle_init_shared_data (
117
- self ._mqtt_listen_topic ,
118
- device_info ,
119
- )
100
+ self ._shared , self ._file_name = coordinator .update_shared_data (device_info )
120
101
self ._start_up_logs ()
121
102
self ._storage_path , self .snapshot_img , self .log_file = self ._init_paths ()
103
+ self ._mqtt_listen_topic = coordinator .vacuum_topic
122
104
self ._attr_unique_id = device_info .get (
123
105
CONF_UNIQUE_ID ,
124
106
get_vacuum_unique_id_from_mqtt_topic (self ._mqtt_listen_topic ),
125
107
)
126
- self ._mqtt = ValetudoConnector ( self . _mqtt_listen_topic , self . hass , self . _shared )
108
+ self ._mqtt = coordinator . connector
127
109
self ._identifiers = device_info .get (CONF_VACUUM_IDENTIFIERS )
128
110
self ._snapshots = Snapshots (self .hass , self ._shared )
129
111
self .Image = None
@@ -144,23 +126,13 @@ def __init__(self, hass, device_info):
144
126
self ._colours .set_initial_colours (device_info )
145
127
# Create the processor for the camera.
146
128
self .processor = CameraProcessor (self .hass , self ._shared )
147
- self ._attr_brand = "MQTT Vacuum Camera"
148
129
149
- @staticmethod
150
- def _handle_init_shared_data (mqtt_listen_topic : str , device_info ):
151
- """Handle the shared data initialization."""
152
- manager , shared , file_name = None , None , None
153
- if mqtt_listen_topic :
154
- manager = CameraSharedManager (
155
- mqtt_listen_topic .split ("/" )[1 ].lower (), device_info
156
- )
157
- shared = manager .get_instance ()
158
- file_name = shared .file_name
159
- _LOGGER .debug (f"Camera { file_name } Starting up.." )
160
- return shared , file_name
130
+ # Listen to the vacuum.start event
131
+ self .hass .bus .async_listen ("vacuum.start" , self .handle_vacuum_start )
161
132
162
133
@staticmethod
163
134
def _start_up_logs ():
135
+ """Logs the machine running the component data"""
164
136
_LOGGER .info (f"System Release: { platform .node ()} , { platform .release ()} " )
165
137
_LOGGER .info (f"System Version: { platform .version ()} " )
166
138
_LOGGER .info (f"System Machine: { platform .machine ()} " )
@@ -172,6 +144,7 @@ def _start_up_logs():
172
144
)
173
145
174
146
def _init_clear_www_folder (self ):
147
+ """Remove PNG and ZIP's stored in HA config WWW"""
175
148
# If enable_snapshots check if for png in www
176
149
if not self ._shared .enable_snapshots and os .path .isfile (
177
150
f"{ self ._directory_path } /www/snapshot_{ self ._file_name } .png"
@@ -263,6 +236,13 @@ def extra_state_attributes(self) -> dict:
263
236
264
237
return attributes
265
238
239
+ async def handle_vacuum_start (self , event ):
240
+ """Handle the vacuum.start event."""
241
+ _LOGGER .debug (f"Received event: { event .event_type } , Data: { event .data } " )
242
+
243
+ # Call the reset_trims service when vacuum.start event occurs
244
+ await self .hass .services .async_call ("mqtt_vacuum_camera" , "reset_trims" )
245
+
266
246
@property
267
247
def should_poll (self ) -> bool :
268
248
"""ON/OFF Camera Polling"""
0 commit comments