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
5d18529f
Commit
5d18529f
authored
5 months ago
by
KERDREUX Jerome
Browse files
Options
Downloads
Patches
Plain Diff
Format
parent
49f2f7a7
Branches
Branches containing commit
No related tags found
1 merge request
!1
First try of type hints
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
devices/protocols/ESPHome/xaal/esphome/bindings.py
+13
-10
13 additions, 10 deletions
devices/protocols/ESPHome/xaal/esphome/bindings.py
devices/weather/OpenWeatherMap/xaal/owm/gw.py
+40
-33
40 additions, 33 deletions
devices/weather/OpenWeatherMap/xaal/owm/gw.py
with
53 additions
and
43 deletions
devices/protocols/ESPHome/xaal/esphome/bindings.py
+
13
−
10
View file @
5d18529f
from
xaal.lib
import
tools
,
Device
from
xaal.schemas
import
devices
from
pprint
import
pprint
from
aioesphomeapi
import
APIClient
,
APIConnectionError
,
model
import
asyncio
import
asyncio
import
logging
import
logging
from
aioesphomeapi
import
APIClient
,
APIConnectionError
,
model
from
xaal.lib
import
Device
,
tools
from
xaal.schemas
import
devices
logging
.
getLogger
(
'
aioesphomeapi
'
).
setLevel
(
logging
.
INFO
)
logging
.
getLogger
(
'
aioesphomeapi
'
).
setLevel
(
logging
.
INFO
)
logger
=
logging
.
getLogger
(
__name__
)
logger
=
logging
.
getLogger
(
__name__
)
...
@@ -18,7 +16,9 @@ class ESPDevice:
...
@@ -18,7 +16,9 @@ class ESPDevice:
self
.
port
=
cfg
.
get
(
'
port
'
,
6053
)
self
.
port
=
cfg
.
get
(
'
port
'
,
6053
)
self
.
key
=
cfg
.
get
(
'
key
'
,
None
)
self
.
key
=
cfg
.
get
(
'
key
'
,
None
)
self
.
passwd
=
cfg
.
get
(
'
passwd
'
,
None
)
self
.
passwd
=
cfg
.
get
(
'
passwd
'
,
None
)
self
.
base_addr
=
tools
.
get_uuid
(
cfg
.
get
(
'
base_addr
'
))
addr
=
tools
.
get_uuid
(
cfg
.
get
(
'
base_addr
'
))
assert
addr
,
"
Invalid base_addr
"
self
.
base_addr
=
addr
self
.
embedded
=
[]
self
.
embedded
=
[]
self
.
disconnected
=
asyncio
.
Event
()
self
.
disconnected
=
asyncio
.
Event
()
...
@@ -91,10 +91,12 @@ def find_device_class(info):
...
@@ -91,10 +91,12 @@ def find_device_class(info):
if
type_
==
model
.
LightInfo
:
if
type_
==
model
.
LightInfo
:
return
Lamp
return
Lamp
elif
type_
==
model
.
SwitchInfo
:
elif
type_
==
model
.
SwitchInfo
:
return
PowerRelay
return
PowerRelay
elif
type_
==
model
.
SensorInfo
:
elif
type_
==
model
.
SensorInfo
:
if
info
.
device_class
==
'
signal_strength
'
:
return
WifiMeter
if
info
.
device_class
==
'
signal_strength
'
:
if
info
.
device_class
==
'
power
'
:
return
PowerMeter
return
WifiMeter
if
info
.
device_class
==
'
power
'
:
return
PowerMeter
elif
type_
==
model
.
BinarySensorInfo
:
elif
type_
==
model
.
BinarySensorInfo
:
return
Contact
return
Contact
...
@@ -121,6 +123,7 @@ class EntityMixin(object):
...
@@ -121,6 +123,7 @@ class EntityMixin(object):
return
self
.
info
.
key
return
self
.
info
.
key
def
setup_device_description
(
self
):
def
setup_device_description
(
self
):
assert
self
.
dev
,
"
Device not setup
"
self
.
dev
.
vendor_id
=
'
ESPHome
'
self
.
dev
.
vendor_id
=
'
ESPHome
'
self
.
dev
.
product_id
=
self
.
info
.
unique_id
self
.
dev
.
product_id
=
self
.
info
.
unique_id
self
.
dev
.
hw_id
=
self
.
info
.
key
self
.
dev
.
hw_id
=
self
.
info
.
key
...
...
This diff is collapsed.
Click to expand it.
devices/weather/OpenWeatherMap/xaal/owm/gw.py
+
40
−
33
View file @
5d18529f
import
platform
import
logging
import
logging
import
platform
import
pyowm
import
pyowm
from
pyowm.exceptions
import
OWMError
from
pyowm.exceptions
import
OWMError
from
xaal.lib
import
tools
from
xaal.schemas
import
devices
from
xaal.lib
import
helpers
from
xaal.lib
import
helpers
,
tools
from
xaal.schemas
import
devices
PACKAGE_NAME
=
"
xaal.owm
"
PACKAGE_NAME
=
"
xaal.owm
"
RATE
=
300
# update every 5 min
RATE
=
300
# update every 5 min
API_KEY
=
'
3a5989bac31472cd41d69e92838bd454
'
API_KEY
=
'
3a5989bac31472cd41d69e92838bd454
'
logger
=
logging
.
getLogger
(
PACKAGE_NAME
)
logger
=
logging
.
getLogger
(
PACKAGE_NAME
)
def
setup_dev
(
dev
):
def
setup_dev
(
dev
):
dev
.
vendor_id
=
"
IHSEV
"
dev
.
vendor_id
=
"
IHSEV
"
dev
.
product_id
=
"
OpenWeatherMap
"
dev
.
product_id
=
"
OpenWeatherMap
"
dev
.
info
=
"
%s@%s
"
%
(
PACKAGE_NAME
,
platform
.
node
())
dev
.
info
=
"
%s@%s
"
%
(
PACKAGE_NAME
,
platform
.
node
())
dev
.
url
=
"
https://www.openweathermap.org
"
dev
.
url
=
"
https://www.openweathermap.org
"
dev
.
version
=
0.3
dev
.
version
=
0.3
return
dev
return
dev
class
GW
:
class
GW
:
def
__init__
(
self
,
engine
):
def
__init__
(
self
,
engine
):
self
.
eng
=
engine
self
.
eng
=
engine
engine
.
on_stop
(
self
.
save_config
)
engine
.
on_stop
(
self
.
save_config
)
cfg
=
tools
.
load_cfg
(
PACKAGE_NAME
)
cfg
=
tools
.
load_cfg
(
PACKAGE_NAME
)
if
cfg
==
None
:
if
cfg
is
None
:
logger
.
info
(
'
New config file
'
)
logger
.
info
(
'
New config file
'
)
cfg
=
tools
.
new_cfg
(
PACKAGE_NAME
)
cfg
=
tools
.
new_cfg
(
PACKAGE_NAME
)
cfg
[
'
config
'
][
'
base_addr
'
]
=
str
(
tools
.
get_random_base_uuid
())
cfg
[
'
config
'
][
'
base_addr
'
]
=
str
(
tools
.
get_random_base_uuid
())
self
.
cfg
=
cfg
self
.
cfg
=
cfg
self
.
setup
()
self
.
setup
()
self
.
update
()
self
.
update
()
def
setup
(
self
):
def
setup
(
self
):
"""
create devices, register ..
"""
"""
create devices, register ..
"""
cfg
=
self
.
cfg
[
'
config
'
]
cfg
=
self
.
cfg
[
'
config
'
]
base_addr
=
tools
.
get_uuid
(
cfg
[
'
base_addr
'
])
base_addr
=
tools
.
get_uuid
(
cfg
[
'
base_addr
'
])
if
base_addr
is
None
:
logger
.
error
(
'
Invalid base_addr
'
)
return
# devices
# devices
d1
=
devices
.
thermometer
(
base_addr
+
0
)
d1
=
devices
.
thermometer
(
base_addr
+
0
)
d2
=
devices
.
hygrometer
(
base_addr
+
1
)
d2
=
devices
.
hygrometer
(
base_addr
+
1
)
d3
=
devices
.
barometer
(
base_addr
+
2
)
d3
=
devices
.
barometer
(
base_addr
+
2
)
d4
=
devices
.
windgauge
(
base_addr
+
3
)
d4
=
devices
.
windgauge
(
base_addr
+
3
)
d4
.
unsupported_attributes
.
append
(
'
gust_angle
'
)
d4
.
unsupported_attributes
.
append
(
'
gust_angle
'
)
d4
.
del_attribute
(
d4
.
get_attribute
(
'
gust_angle
'
))
gust
=
d4
.
get_attribute
(
'
gust_angle
'
)
self
.
devs
=
[
d1
,
d2
,
d3
,
d4
]
if
gust
:
d4
.
del_attribute
(
gust
)
self
.
devs
=
[
d1
,
d2
,
d3
,
d4
]
# gw
# gw
gw
=
devices
.
gateway
(
tools
.
get_uuid
(
cfg
[
'
addr
'
]))
gw
=
devices
.
gateway
(
tools
.
get_uuid
(
cfg
[
'
addr
'
]))
gw
.
attributes
[
'
embedded
'
]
=
[
dev
.
address
for
dev
in
self
.
devs
]
gw
.
attributes
[
'
embedded
'
]
=
[
dev
.
address
for
dev
in
self
.
devs
]
group
=
base_addr
+
0x
ff
group
=
base_addr
+
0x
FF
for
dev
in
(
self
.
devs
+
[
gw
,]
)
:
for
dev
in
self
.
devs
+
[
gw
,]:
setup_dev
(
dev
)
setup_dev
(
dev
)
if
dev
!=
gw
:
if
dev
!=
gw
:
dev
.
group_id
=
group
dev
.
group_id
=
group
self
.
eng
.
add_devices
(
self
.
devs
+
[
gw
,])
self
.
eng
.
add_devices
(
self
.
devs
+
[
gw
,])
# OWM stuff
# OWM stuff
self
.
eng
.
add_timer
(
self
.
update
,
RATE
)
self
.
eng
.
add_timer
(
self
.
update
,
RATE
)
# API Key
# API Key
api_key
=
cfg
.
get
(
'
api_key
'
,
None
)
api_key
=
cfg
.
get
(
'
api_key
'
,
None
)
if
not
api_key
:
if
not
api_key
:
cfg
[
'
api_key
'
]
=
api_key
=
API_KEY
cfg
[
'
api_key
'
]
=
api_key
=
API_KEY
# Place
# Place
self
.
place
=
cfg
.
get
(
'
place
'
,
None
)
self
.
place
=
cfg
.
get
(
'
place
'
,
None
)
if
not
self
.
place
:
if
not
self
.
place
:
cfg
[
'
place
'
]
=
self
.
place
=
'
Brest,FR
'
cfg
[
'
place
'
]
=
self
.
place
=
'
Brest,FR
'
# We are ready
# We are ready
...
@@ -78,18 +83,19 @@ class GW:
...
@@ -78,18 +83,19 @@ class GW:
try
:
try
:
self
.
_update
()
self
.
_update
()
except
OWMError
as
e
:
except
OWMError
as
e
:
logger
.
warn
(
e
)
logger
.
warn
ing
(
e
)
def
_update
(
self
):
def
_update
(
self
):
weather
=
self
.
owm
.
weather_at_place
(
self
.
place
).
get_weather
()
weather
=
self
.
owm
.
weather_at_place
(
self
.
place
).
get_weather
()
self
.
devs
[
0
].
attributes
[
'
temperature
'
]
=
round
(
weather
.
get_temperature
(
unit
=
'
celsius
'
).
get
(
'
temp
'
,
None
),
1
)
self
.
devs
[
0
].
attributes
[
'
temperature
'
]
=
round
(
weather
.
get_temperature
(
unit
=
'
celsius
'
).
get
(
'
temp
'
,
None
),
1
)
self
.
devs
[
1
].
attributes
[
'
humidity
'
]
=
weather
.
get_humidity
()
self
.
devs
[
1
].
attributes
[
'
humidity
'
]
=
weather
.
get_humidity
()
self
.
devs
[
2
].
attributes
[
'
pressure
'
]
=
weather
.
get_pressure
().
get
(
'
press
'
,
None
)
self
.
devs
[
2
].
attributes
[
'
pressure
'
]
=
weather
.
get_pressure
().
get
(
'
press
'
,
None
)
wind
=
weather
.
get_wind
().
get
(
'
speed
'
,
None
)
wind
=
weather
.
get_wind
().
get
(
'
speed
'
,
None
)
if
wind
:
wind
=
round
(
wind
*
3600
/
1000
,
1
)
# m/s => km/h
if
wind
:
wind
=
round
(
wind
*
3600
/
1000
,
1
)
# m/s => km/h
self
.
devs
[
3
].
attributes
[
'
wind_strength
'
]
=
wind
self
.
devs
[
3
].
attributes
[
'
wind_strength
'
]
=
wind
self
.
devs
[
3
].
attributes
[
'
wind_angle
'
]
=
weather
.
get_wind
().
get
(
'
deg
'
,
None
)
self
.
devs
[
3
].
attributes
[
'
wind_angle
'
]
=
weather
.
get_wind
().
get
(
'
deg
'
,
None
)
self
.
devs
[
3
].
attributes
[
'
gust_strength
'
]
=
weather
.
get_wind
().
get
(
'
gust
'
,
None
)
self
.
devs
[
3
].
attributes
[
'
gust_strength
'
]
=
weather
.
get_wind
().
get
(
'
gust
'
,
None
)
def
save_config
(
self
):
def
save_config
(
self
):
cfg
=
tools
.
load_cfg
(
PACKAGE_NAME
)
cfg
=
tools
.
load_cfg
(
PACKAGE_NAME
)
...
@@ -97,6 +103,7 @@ class GW:
...
@@ -97,6 +103,7 @@ class GW:
logger
.
info
(
'
Saving configuration file
'
)
logger
.
info
(
'
Saving configuration file
'
)
self
.
cfg
.
write
()
self
.
cfg
.
write
()
def
setup
(
engine
):
def
setup
(
engine
):
gw
=
GW
(
engine
)
gw
=
GW
(
engine
)
return
True
return
True
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