1
1
"""Support for Rfplayer devices."""
2
- import asyncio
2
+ from asyncio import timeout
3
3
from collections import defaultdict
4
4
import copy
5
5
import logging
6
6
7
- import async_timeout
8
7
from serial import SerialException
9
- from homeassistant .util import slugify
10
8
import voluptuous as vol
11
9
10
+ from homeassistant .config_entries import ConfigEntry
12
11
from homeassistant .const import (
13
12
ATTR_ENTITY_ID ,
14
13
ATTR_STATE ,
19
18
CONF_PROTOCOL ,
20
19
EVENT_HOMEASSISTANT_STOP ,
21
20
)
22
- from homeassistant .core import CoreState , callback
21
+ from homeassistant .core import CoreState , HomeAssistant , callback
22
+ from homeassistant .helpers import device_registry as dr
23
23
import homeassistant .helpers .config_validation as cv
24
+ from homeassistant .helpers .device_registry import DeviceInfo
24
25
from homeassistant .helpers .dispatcher import (
25
26
async_dispatcher_connect ,
26
27
async_dispatcher_send ,
27
28
)
28
- from homeassistant .helpers import device_registry as dr
29
- from homeassistant .helpers .entity import DeviceInfo
30
29
from homeassistant .helpers .restore_state import RestoreEntity
31
30
32
31
from .const import (
@@ -76,17 +75,19 @@ def identify_event_type(event):
76
75
return "unknown"
77
76
78
77
79
- async def async_setup_entry (hass , entry ) :
78
+ async def async_setup_entry (hass : HomeAssistant , entry : ConfigEntry ) -> bool :
80
79
"""Set up GCE RFPlayer from a config entry."""
80
+ hass .data .setdefault (DOMAIN , {})
81
+
81
82
config = entry .data
82
83
options = entry .options
83
84
84
85
async def async_send_command (call ):
85
86
"""Send Rfplayer command."""
86
87
_LOGGER .debug ("Rfplayer send command for %s" , str (call .data ))
87
88
if not await hass .data [DOMAIN ][RFPLAYER_PROTOCOL ].send_command_ack (
88
- protocol = call .data [CONF_PROTOCOL ],
89
- command = call .data [CONF_COMMAND ],
89
+ call .data [CONF_PROTOCOL ],
90
+ call .data [CONF_COMMAND ],
90
91
device_address = call .data .get (CONF_DEVICE_ADDRESS ),
91
92
device_id = call .data .get (CONF_DEVICE_ID ),
92
93
):
@@ -138,17 +139,15 @@ def event_callback(event):
138
139
# Propagate event to every entity matching the device id
139
140
_LOGGER .debug ("passing event to %s" , entity_id )
140
141
async_dispatcher_send (hass , SIGNAL_HANDLE_EVENT .format (entity_id ), event )
142
+ elif event_type in hass .data [DOMAIN ][DATA_DEVICE_REGISTER ]:
143
+ _LOGGER .debug ("device_id not known, adding new device" )
144
+ hass .data [DOMAIN ][DATA_ENTITY_LOOKUP ][event_type ][event_id ] = event
145
+ _add_device_to_base_config (event , event_id )
146
+ hass .async_create_task (
147
+ hass .data [DOMAIN ][DATA_DEVICE_REGISTER ][event_type ](event )
148
+ )
141
149
else :
142
- # If device is not yet known, register with platform (if loaded)
143
- if event_type in hass .data [DOMAIN ][DATA_DEVICE_REGISTER ]:
144
- _LOGGER .debug ("device_id not known, adding new device" )
145
- hass .data [DOMAIN ][DATA_ENTITY_LOOKUP ][event_type ][event_id ] = event
146
- _add_device_to_base_config (event , event_id )
147
- hass .async_create_task (
148
- hass .data [DOMAIN ][DATA_DEVICE_REGISTER ][event_type ](event )
149
- )
150
- else :
151
- _LOGGER .debug ("device_id not known and automatic add disabled" )
150
+ _LOGGER .debug ("device_id not known and automatic add disabled" )
152
151
153
152
@callback
154
153
def _add_device_to_base_config (event , event_id ):
@@ -182,14 +181,10 @@ async def connect():
182
181
)
183
182
184
183
try :
185
- with async_timeout . timeout (CONNECTION_TIMEOUT ):
184
+ with timeout (CONNECTION_TIMEOUT ):
186
185
transport , protocol = await connection
187
186
188
- except (
189
- SerialException ,
190
- OSError ,
191
- asyncio .TimeoutError ,
192
- ) as exc :
187
+ except (TimeoutError , SerialException , OSError ) as exc :
193
188
reconnect_interval = config [CONF_RECONNECT_INTERVAL ]
194
189
_LOGGER .exception (
195
190
"Error connecting to Rfplayer, reconnecting in %s" , reconnect_interval
@@ -240,8 +235,6 @@ class RfplayerDevice(RestoreEntity):
240
235
Contains the common logic for Rfplayer entities.
241
236
"""
242
237
243
- platform = None
244
- _state = None
245
238
_available = True
246
239
247
240
def __init__ (
@@ -251,22 +244,22 @@ def __init__(
251
244
device_id = None ,
252
245
initial_event = None ,
253
246
name = None ,
254
- ):
247
+ ) -> None :
255
248
"""Initialize the device."""
256
249
# Rflink specific attributes for every component type
257
250
self ._initial_event = initial_event
258
251
self ._protocol = protocol
259
252
self ._device_id = device_id
260
253
self ._device_address = device_address
261
254
self ._event = None
262
- self ._state : bool = None
263
255
self ._attr_assumed_state = True
264
- if name is not None :
256
+ self ._attr_unique_id = "_" .join (
257
+ [self ._protocol , self ._device_address or self ._device_id ]
258
+ )
259
+ if name :
265
260
self ._attr_name = name
266
- self ._attr_unique_id = slugify (f"{ protocol } _{ name } " )
267
261
else :
268
- self ._attr_name = f"{ protocol } { device_id or device_address } "
269
- self ._attr_unique_id = slugify (f"{ protocol } _{ device_id or device_address } " )
262
+ self ._attr_name = f"{ protocol } { device_address or device_id } "
270
263
271
264
async def _async_send_command (self , command , * args ):
272
265
rfplayer = self .hass .data [DOMAIN ][RFPLAYER_PROTOCOL ]
@@ -298,7 +291,7 @@ def handle_event_callback(self, event):
298
291
299
292
def _handle_event (self , event ):
300
293
"""Platform specific event handler."""
301
- raise NotImplementedError ()
294
+ raise NotImplementedError
302
295
303
296
@property
304
297
def should_poll (self ):
0 commit comments