Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Python
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
xAAL
Code
Python
Commits
cf59ffab
Commit
cf59ffab
authored
4 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Format and fix some pyright
parent
270f48c5
No related branches found
No related tags found
1 merge request
!1
First try of type hints
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
apps/homeassistant/xaal/bridge.py
+32
-22
32 additions, 22 deletions
apps/homeassistant/xaal/bridge.py
with
32 additions
and
22 deletions
apps/homeassistant/xaal/bridge.py
+
32
−
22
View file @
cf59ffab
...
@@ -24,9 +24,11 @@ import logging
...
@@ -24,9 +24,11 @@ import logging
_LOGGER
=
logging
.
getLogger
(
__name__
)
_LOGGER
=
logging
.
getLogger
(
__name__
)
UNSUPPORTED_TYPES
=
[
'
cli
'
,
'
hmi
'
,
'
logger
'
]
UNSUPPORTED_TYPES
=
[
'
cli
'
,
'
hmi
'
,
'
logger
'
]
UPDATE_NOTIFICATIONS
=
[
Notification
.
attribute_change
,
UPDATE_NOTIFICATIONS
=
[
Notification
.
description_change
,
Notification
.
attribute_change
,
Notification
.
metadata_change
]
Notification
.
description_change
,
Notification
.
metadata_change
,
]
class
XAALEntity
(
Entity
):
class
XAALEntity
(
Entity
):
...
@@ -43,7 +45,7 @@ class XAALEntity(Entity):
...
@@ -43,7 +45,7 @@ class XAALEntity(Entity):
# Life cycle
# Life cycle
#####################################################
#####################################################
def
setup
(
self
):
def
setup
(
self
):
"""
Use setup to tweak a entity at creation
"""
"""
Use setup to tweak a entity at creation
"""
pass
pass
async
def
async_added_to_hass
(
self
)
->
None
:
async
def
async_added_to_hass
(
self
)
->
None
:
...
@@ -58,7 +60,7 @@ class XAALEntity(Entity):
...
@@ -58,7 +60,7 @@ class XAALEntity(Entity):
def
fix_device
(
self
,
device
:
MonitorDevice
)
->
None
:
def
fix_device
(
self
,
device
:
MonitorDevice
)
->
None
:
"""
The xAAL device is back after an auto-wash, so we need to switch,
"""
The xAAL device is back after an auto-wash, so we need to switch,
to the new MonitorDevice.
to the new MonitorDevice.
"""
"""
self
.
_dev
=
device
self
.
_dev
=
device
self
.
_attr_available
=
True
self
.
_attr_available
=
True
...
@@ -78,7 +80,7 @@ class XAALEntity(Entity):
...
@@ -78,7 +80,7 @@ class XAALEntity(Entity):
"
manufacturer
"
:
dev
.
description
.
get
(
"
vendor_id
"
),
"
manufacturer
"
:
dev
.
description
.
get
(
"
vendor_id
"
),
"
sw_version
"
:
dev
.
description
.
get
(
"
version
"
),
"
sw_version
"
:
dev
.
description
.
get
(
"
version
"
),
"
hw_version
"
:
dev
.
description
.
get
(
"
hw_id
"
),
"
hw_version
"
:
dev
.
description
.
get
(
"
hw_id
"
),
"
suggested_area
"
:
dev
.
db
.
get
(
"
location
"
)
"
suggested_area
"
:
dev
.
db
.
get
(
"
location
"
)
,
}
}
#####################################################
#####################################################
...
@@ -86,10 +88,14 @@ class XAALEntity(Entity):
...
@@ -86,10 +88,14 @@ class XAALEntity(Entity):
#####################################################
#####################################################
def
send_request
(
self
,
action
:
str
,
body
:
Dict
[
str
,
Any
]
|
None
=
None
)
->
None
:
def
send_request
(
self
,
action
:
str
,
body
:
Dict
[
str
,
Any
]
|
None
=
None
)
->
None
:
_LOGGER
.
debug
(
f
"
{
self
}
{
action
}
{
body
}
"
)
_LOGGER
.
debug
(
f
"
{
self
}
{
action
}
{
body
}
"
)
self
.
_bridge
.
send_request
([
self
.
_dev
.
address
,
],
action
,
body
)
self
.
_bridge
.
send_request
(
[
self
.
_dev
.
address
],
action
,
body
,
)
def
get_attribute
(
self
,
name
:
str
,
default
:
Dict
[
str
,
Any
]
=
None
)
->
Any
:
def
get_attribute
(
self
,
name
:
str
,
default
:
Dict
[
str
,
Any
]
=
None
)
->
Any
:
"""
return a attribute for xAAL device
"""
"""
return a attribute for xAAL device
"""
return
self
.
_dev
.
attributes
.
get
(
name
,
default
)
return
self
.
_dev
.
attributes
.
get
(
name
,
default
)
@property
@property
...
@@ -100,7 +106,7 @@ class XAALEntity(Entity):
...
@@ -100,7 +106,7 @@ class XAALEntity(Entity):
# Entity properties
# Entity properties
#####################################################
#####################################################
def
short_type
(
self
)
->
str
:
def
short_type
(
self
)
->
str
:
"""
return a fake device class for entity that doesn
'
t have one
"""
"""
return a fake device class for entity that doesn
'
t have one
"""
# this apply for light, button
# this apply for light, button
# NOTE: I don't know why some HASS entities don't have a device class
# NOTE: I don't know why some HASS entities don't have a device class
return
self
.
_dev
.
dev_type
.
split
(
'
.
'
)[
0
]
return
self
.
_dev
.
dev_type
.
split
(
'
.
'
)[
0
]
...
@@ -140,7 +146,7 @@ class EntityFactory(object):
...
@@ -140,7 +146,7 @@ class EntityFactory(object):
self
.
_binding
=
binding
self
.
_binding
=
binding
def
build_entities
(
self
,
device
:
MonitorDevice
)
->
bool
:
def
build_entities
(
self
,
device
:
MonitorDevice
)
->
bool
:
"""
return True if this factory managed to build some entities
"""
"""
return True if this factory managed to build some entities
"""
result
=
[]
result
=
[]
for
b_type
in
self
.
_binding
.
keys
():
for
b_type
in
self
.
_binding
.
keys
():
if
device
.
dev_type
.
startswith
(
b_type
):
if
device
.
dev_type
.
startswith
(
b_type
):
...
@@ -154,11 +160,12 @@ class EntityFactory(object):
...
@@ -154,11 +160,12 @@ class EntityFactory(object):
return
False
return
False
def
async_setup_factory
(
hass
:
HomeAssistant
,
def
async_setup_factory
(
config_entry
:
ConfigEntry
,
hass
:
HomeAssistant
,
async_add_entities
:
AddEntitiesCallback
,
config_entry
:
ConfigEntry
,
binding
:
dict
)
->
None
:
async_add_entities
:
AddEntitiesCallback
,
binding
:
dict
,
)
->
None
:
bridge
:
Bridge
=
hass
.
data
[
DOMAIN
][
config_entry
.
entry_id
]
bridge
:
Bridge
=
hass
.
data
[
DOMAIN
][
config_entry
.
entry_id
]
factory
=
EntityFactory
(
bridge
,
async_add_entities
,
binding
)
factory
=
EntityFactory
(
bridge
,
async_add_entities
,
binding
)
bridge
.
add_factory
(
factory
)
bridge
.
add_factory
(
factory
)
...
@@ -169,6 +176,9 @@ def async_setup_factory(hass: HomeAssistant,
...
@@ -169,6 +176,9 @@ def async_setup_factory(hass: HomeAssistant,
def
filter_msg
(
msg
:
Message
)
->
bool
:
def
filter_msg
(
msg
:
Message
)
->
bool
:
if
msg
.
dev_type
is
None
:
# This should not happen
return
False
m_type
=
msg
.
dev_type
.
split
(
'
.
'
)[
0
]
m_type
=
msg
.
dev_type
.
split
(
'
.
'
)[
0
]
if
m_type
in
UNSUPPORTED_TYPES
:
if
m_type
in
UNSUPPORTED_TYPES
:
return
False
return
False
...
@@ -176,7 +186,6 @@ def filter_msg(msg: Message) -> bool:
...
@@ -176,7 +186,6 @@ def filter_msg(msg: Message) -> bool:
class
Bridge
(
object
):
class
Bridge
(
object
):
def
__init__
(
self
,
hass
:
HomeAssistant
,
db_server
)
->
None
:
def
__init__
(
self
,
hass
:
HomeAssistant
,
db_server
)
->
None
:
"""
Init xAAL bridge.
"""
"""
Init xAAL bridge.
"""
self
.
hass
=
hass
self
.
hass
=
hass
...
@@ -207,7 +216,7 @@ class Bridge(object):
...
@@ -207,7 +216,7 @@ class Bridge(object):
return
self
.
_mon
.
boot_finished
return
self
.
_mon
.
boot_finished
async
def
wait_is_ready
(
self
)
->
bool
:
async
def
wait_is_ready
(
self
)
->
bool
:
"""
Wait the monitor to received all devices infos
"""
"""
Wait the monitor to received all devices infos
"""
while
1
:
while
1
:
if
self
.
_mon
.
boot_finished
:
if
self
.
_mon
.
boot_finished
:
return
True
return
True
...
@@ -233,7 +242,8 @@ class Bridge(object):
...
@@ -233,7 +242,8 @@ class Bridge(object):
self
.
_eng
.
send_request
(
self
.
_dev
,
targets
,
action
,
body
)
self
.
_eng
.
send_request
(
self
.
_dev
,
targets
,
action
,
body
)
def
ha_update_db
(
self
,
body
:
dict
):
def
ha_update_db
(
self
,
body
:
dict
):
self
.
send_request
([
self
.
_mon
.
db_server
],
'
update_keys_values
'
,
body
)
if
self
.
_mon
.
db_server
:
self
.
send_request
([
self
.
_mon
.
db_server
],
'
update_keys_values
'
,
body
)
#####################################################
#####################################################
# xAAL Monitor events & callbacks
# xAAL Monitor events & callbacks
...
@@ -305,10 +315,10 @@ class Bridge(object):
...
@@ -305,10 +315,10 @@ class Bridge(object):
except
KeyError
:
except
KeyError
:
# device already auto-washed or
# device already auto-washed or
# and old entity
# and old entity
_LOGGER
.
warn
(
f
"
Unknow entity w/ addr
{
addr
}
"
)
_LOGGER
.
warn
ing
(
f
"
Unknow entity w/ addr
{
addr
}
"
)
def
get_entities
(
self
,
addr
:
bindings
.
UUID
)
->
List
[
XAALEntity
]
|
None
:
def
get_entities
(
self
,
addr
:
bindings
.
UUID
)
->
List
[
XAALEntity
]
|
None
:
"""
return entities for a given xAAL address
"""
"""
return entities for a given xAAL address
"""
return
self
.
_entities
.
get
(
addr
)
return
self
.
_entities
.
get
(
addr
)
def
get_entity_by_id
(
self
,
entity_id
:
str
)
->
XAALEntity
|
None
:
def
get_entity_by_id
(
self
,
entity_id
:
str
)
->
XAALEntity
|
None
:
...
@@ -342,7 +352,7 @@ class Bridge(object):
...
@@ -342,7 +352,7 @@ class Bridge(object):
return
[
dev
.
address
for
dev
in
self
.
_mon
.
devices
.
get_with_group
(
addr
)]
return
[
dev
.
address
for
dev
in
self
.
_mon
.
devices
.
get_with_group
(
addr
)]
def
ha_remove_device
(
self
,
ident
:
str
)
->
None
:
def
ha_remove_device
(
self
,
ident
:
str
)
->
None
:
"""
User asked to remove an HA device, we need to find out the entites
"""
"""
User asked to remove an HA device, we need to find out the entites
"""
for
addr
in
self
.
ident_to_address
(
ident
):
for
addr
in
self
.
ident_to_address
(
ident
):
self
.
remove_entities
(
addr
)
self
.
remove_entities
(
addr
)
...
@@ -350,7 +360,7 @@ class Bridge(object):
...
@@ -350,7 +360,7 @@ class Bridge(object):
# Factories
# Factories
#####################################################
#####################################################
def
add_factory
(
self
,
factory
:
EntityFactory
):
def
add_factory
(
self
,
factory
:
EntityFactory
):
"""
register a new platform factory
"""
"""
register a new platform factory
"""
self
.
_factories
.
append
(
factory
)
self
.
_factories
.
append
(
factory
)
def
remove_factory
(
self
,
factory
:
EntityFactory
):
def
remove_factory
(
self
,
factory
:
EntityFactory
):
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment