state_spec.rb 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. require 'api_client'
  2. RSpec.describe 'State' do
  3. before(:all) do
  4. @client = ApiClient.new(ENV.fetch('ESPMH_HOSTNAME'), ENV.fetch('ESPMH_TEST_DEVICE_ID_BASE'))
  5. @client.upload_json('/settings', 'settings.json')
  6. end
  7. before(:each) do
  8. @id_params = {
  9. id: @client.generate_id,
  10. type: 'rgb_cct',
  11. group_id: 1
  12. }
  13. @client.delete_state(@id_params)
  14. end
  15. context 'initial state' do
  16. it 'should assume white mode for device types that are white-only' do
  17. %w(cct fut091).each do |type|
  18. id = @id_params.merge(type: type)
  19. @client.delete_state(id)
  20. state = @client.patch_state({status: 'ON'}, id)
  21. expect(state['bulb_mode']).to eq('white'), "it should assume white mode for #{type}"
  22. end
  23. end
  24. it 'should assume color mode for device types that are rgb-only' do
  25. %w(rgb).each do |type|
  26. id = @id_params.merge(type: type)
  27. @client.delete_state(id)
  28. state = @client.patch_state({status: 'ON'}, id)
  29. expect(state['bulb_mode']).to eq('color'), "it should assume color mode for #{type}"
  30. end
  31. end
  32. end
  33. context 'toggle command' do
  34. it 'should toggle ON to OFF' do
  35. init_state = @client.patch_state({'status' => 'ON'}, @id_params)
  36. expect(init_state['status']).to eq('ON')
  37. next_state = @client.patch_state({'command' => 'toggle'}, @id_params)
  38. expect(next_state['status']).to eq('OFF')
  39. end
  40. it 'should toggle OFF to ON' do
  41. init_state = @client.patch_state({'status' => 'OFF'}, @id_params)
  42. expect(init_state['status']).to eq('OFF')
  43. next_state = @client.patch_state({'command' => 'toggle'}, @id_params)
  44. expect(next_state['status']).to eq('ON')
  45. end
  46. end
  47. context 'night mode command' do
  48. StateHelpers::ALL_REMOTE_TYPES
  49. .reject { |x| %w(rgb).include?(x) } # Night mode not supported for these types
  50. .each do |type|
  51. it "should affect state when bulb is OFF for #{type}" do
  52. params = @id_params.merge(type: type)
  53. @client.delete_state(params)
  54. state = @client.patch_state({'command' => 'night_mode'}, params)
  55. expect(state['bulb_mode']).to eq('night')
  56. expect(state['effect']).to eq('night_mode')
  57. end
  58. end
  59. StateHelpers::ALL_REMOTE_TYPES
  60. .reject { |x| %w(rgb).include?(x) } # Night mode not supported for these types
  61. .each do |type|
  62. it "should affect state when bulb is ON for #{type}" do
  63. params = @id_params.merge(type: type)
  64. @client.delete_state(params)
  65. @client.patch_state({'status' => 'ON'}, params)
  66. state = @client.patch_state({'command' => 'night_mode'}, params)
  67. # RGBW bulbs have to be OFF in order for night mode to take affect
  68. expect(state['status']).to eq('ON') if type != 'rgbw'
  69. expect(state['bulb_mode']).to eq('night')
  70. expect(state['effect']).to eq('night_mode')
  71. end
  72. end
  73. it 'should revert to previous mode when status is toggled' do
  74. @client.patch_state({'status' => 'ON', 'kelvin' => 100}, @id_params)
  75. state = @client.patch_state({'command' => 'night_mode'}, @id_params)
  76. expect(state['effect']).to eq('night_mode')
  77. state = @client.patch_state({'status' => 'OFF'}, @id_params)
  78. expect(state['bulb_mode']).to eq('white')
  79. expect(state['kelvin']).to eq(100)
  80. @client.patch_state({'status' => 'ON', 'hue' => 0}, @id_params)
  81. state = @client.patch_state({'command' => 'night_mode'}, @id_params)
  82. expect(state['effect']).to eq('night_mode')
  83. state = @client.patch_state({'status' => 'OFF'}, @id_params)
  84. expect(state['bulb_mode']).to eq('color')
  85. expect(state['hue']).to eq(0)
  86. end
  87. end
  88. context 'deleting' do
  89. it 'should support deleting state' do
  90. desired_state = {
  91. 'status' => 'ON',
  92. 'level' => 10,
  93. 'hue' => 49,
  94. 'saturation' => 20
  95. }
  96. @client.patch_state(desired_state, @id_params)
  97. resulting_state = @client.get_state(@id_params)
  98. expect(resulting_state).to_not be_empty
  99. @client.delete_state(@id_params)
  100. resulting_state = @client.get_state(@id_params)
  101. expect(resulting_state).to be_empty
  102. end
  103. end
  104. context 'persistence' do
  105. it 'should persist parameters' do
  106. desired_state = {
  107. 'status' => 'ON',
  108. 'level' => 100,
  109. 'hue' => 0,
  110. 'saturation' => 100
  111. }
  112. @client.patch_state(desired_state, @id_params)
  113. patched_state = @client.get_state(@id_params)
  114. states_are_equal(desired_state, patched_state)
  115. desired_state = {
  116. 'status' => 'ON',
  117. 'level' => 10,
  118. 'hue' => 49,
  119. 'saturation' => 20
  120. }
  121. @client.patch_state(desired_state, @id_params)
  122. patched_state = @client.get_state(@id_params)
  123. states_are_equal(desired_state, patched_state)
  124. end
  125. it 'should affect member groups when changing group 0' do
  126. group_0_params = @id_params.merge(group_id: 0)
  127. desired_state = {
  128. 'status' => 'ON',
  129. 'level' => 100,
  130. 'hue' => 0,
  131. 'saturation' => 100
  132. }
  133. @client.patch_state(desired_state, group_0_params)
  134. individual_state = desired_state.merge('level' => 10)
  135. patched_state = @client.patch_state(individual_state, @id_params)
  136. expect(patched_state).to_not eq(desired_state)
  137. states_are_equal(individual_state, patched_state)
  138. group_4_state = @client.get_state(group_0_params.merge(group_id: 4))
  139. states_are_equal(desired_state, group_4_state)
  140. @client.patch_state(desired_state, group_0_params)
  141. group_1_state = @client.get_state(group_0_params.merge(group_id: 1))
  142. states_are_equal(desired_state, group_1_state)
  143. end
  144. it 'should keep group 0 state' do
  145. group_0_params = @id_params.merge(group_id: 0)
  146. desired_state = {
  147. 'status' => 'ON',
  148. 'level' => 100,
  149. 'hue' => 0,
  150. 'saturation' => 100
  151. }
  152. patched_state = @client.patch_state(desired_state, group_0_params)
  153. states_are_equal(desired_state, patched_state)
  154. end
  155. it 'should clear group 0 state after member group state changes' do
  156. group_0_params = @id_params.merge(group_id: 0)
  157. desired_state = {
  158. 'status' => 'ON',
  159. 'level' => 100,
  160. 'kelvin' => 100
  161. }
  162. @client.patch_state(desired_state, group_0_params)
  163. @client.patch_state(desired_state.merge('kelvin' => 10), @id_params)
  164. resulting_state = @client.get_state(group_0_params)
  165. expect(resulting_state.keys).to_not include('kelvin')
  166. states_are_equal(desired_state.reject { |x| x == 'kelvin' }, resulting_state)
  167. end
  168. it 'should not clear group 0 state when updating member group state if value is the same' do
  169. group_0_params = @id_params.merge(group_id: 0)
  170. desired_state = {
  171. 'status' => 'ON',
  172. 'level' => 100,
  173. 'kelvin' => 100
  174. }
  175. @client.patch_state(desired_state, group_0_params)
  176. @client.patch_state(desired_state.merge('kelvin' => 100), @id_params)
  177. resulting_state = @client.get_state(group_0_params)
  178. expect(resulting_state).to include('kelvin')
  179. states_are_equal(desired_state, resulting_state)
  180. end
  181. it 'changing member state mode and then changing level should preserve group 0 brightness for original mode' do
  182. group_0_params = @id_params.merge(group_id: 0)
  183. desired_state = {
  184. 'status' => 'ON',
  185. 'level' => 100,
  186. 'hue' => 0,
  187. 'saturation' => 100
  188. }
  189. @client.delete_state(group_0_params)
  190. @client.patch_state(desired_state, group_0_params)
  191. # color -> white mode. should not have brightness because brightness will
  192. # have been previously unknown to group 0.
  193. @client.patch_state(desired_state.merge('color_temp' => 253, 'level' => 11), @id_params)
  194. resulting_state = @client.get_state(group_0_params)
  195. expect(resulting_state.keys).to_not include('level')
  196. # color -> effect mode. same as above
  197. @client.patch_state(desired_state, group_0_params)
  198. @client.patch_state(desired_state.merge('mode' => 0), @id_params)
  199. resulting_state = @client.get_state(group_0_params)
  200. expect(resulting_state).to_not include('level')
  201. # white mode -> color.
  202. white_mode_desired_state = {'status' => 'ON', 'color_temp' => 253, 'level' => 11}
  203. @client.patch_state(white_mode_desired_state, group_0_params)
  204. @client.patch_state({'hue' => 10}, @id_params)
  205. resulting_state = @client.get_state(group_0_params)
  206. expect(resulting_state).to_not include('level')
  207. @client.patch_state({'hue' => 10}, group_0_params)
  208. resulting_state = @client.get_state(group_0_params)
  209. expect(resulting_state['level']).to eq(100)
  210. # white mode -> effect mode. level never set for group 0, so level should
  211. # level should be present.
  212. @client.patch_state(white_mode_desired_state, group_0_params)
  213. @client.patch_state({'mode' => 0}, @id_params)
  214. resulting_state = @client.get_state(group_0_params)
  215. expect(resulting_state).to_not include('level')
  216. # effect mode -> color. same as white mode -> color
  217. effect_mode_desired_state = {'status' => 'ON', 'mode' => 0, 'level' => 100}
  218. @client.patch_state(effect_mode_desired_state, group_0_params)
  219. @client.patch_state({'hue' => 10}, @id_params)
  220. resulting_state = @client.get_state(group_0_params)
  221. expect(resulting_state).to_not include('level')
  222. # effect mode -> white
  223. @client.patch_state(effect_mode_desired_state, group_0_params)
  224. @client.patch_state({'color_temp' => 253}, @id_params)
  225. resulting_state = @client.get_state(group_0_params)
  226. expect(resulting_state).to_not include('level')
  227. end
  228. end
  229. context 'fields' do
  230. it 'should support on/off' do
  231. @client.patch_state({status: 'on'}, @id_params)
  232. expect(@client.get_state(@id_params)['status']).to eq('ON')
  233. # test "state", which is an alias for "status"
  234. @client.patch_state({state: 'off'}, @id_params)
  235. expect(@client.get_state(@id_params)['status']).to eq('OFF')
  236. end
  237. it 'should support boolean values for status' do
  238. # test boolean value "true", which should be the same as "ON".
  239. @client.patch_state({status: true}, @id_params)
  240. expect(@client.get_state(@id_params)['status']).to eq('ON')
  241. @client.patch_state({state: false}, @id_params)
  242. expect(@client.get_state(@id_params)['status']).to eq('OFF')
  243. end
  244. it 'should support the color field' do
  245. desired_state = {
  246. 'hue' => 0,
  247. 'saturation' => 100,
  248. 'status' => 'ON'
  249. }
  250. @client.patch_state(
  251. desired_state.merge(hue: 100),
  252. @id_params
  253. )
  254. @client.patch_state(
  255. { color: '255,0,0' },
  256. @id_params
  257. )
  258. state = @client.get_state(@id_params)
  259. expect(state.keys).to include(*desired_state.keys)
  260. expect(state.select { |x| desired_state.include?(x) } ).to eq(desired_state)
  261. @client.patch_state(
  262. { color: {r: 0, g: 255, b: 0} },
  263. @id_params
  264. )
  265. state = @client.get_state(@id_params)
  266. desired_state.merge!('hue' => 120)
  267. expect(state.keys).to include(*desired_state.keys)
  268. expect(state.select { |x| desired_state.include?(x) } ).to eq(desired_state)
  269. end
  270. it 'should support separate brightness fields for different modes' do
  271. desired_state = {
  272. 'hue' => 0,
  273. 'level' => 50
  274. }
  275. @client.patch_state(desired_state, @id_params)
  276. result = @client.get_state(@id_params)
  277. expect(result['bulb_mode']).to eq('color')
  278. expect(result['level']).to eq(50)
  279. @client.patch_state({'kelvin' => 100}, @id_params)
  280. @client.patch_state({'level' => 70}, @id_params)
  281. result = @client.get_state(@id_params)
  282. expect(result['bulb_mode']).to eq('white')
  283. expect(result['level']).to eq(70)
  284. @client.patch_state({'hue' => 0}, @id_params)
  285. result = @client.get_state(@id_params)
  286. expect(result['bulb_mode']).to eq('color')
  287. # Should retain previous brightness
  288. expect(result['level']).to eq(50)
  289. end
  290. end
  291. context 'increment/decrement commands' do
  292. it 'should assume state after sufficiently many down commands' do
  293. id = @id_params.merge(type: 'cct')
  294. @client.delete_state(id)
  295. @client.patch_state({status: 'on'}, id)
  296. expect(@client.get_state(id)).to_not include('brightness', 'kelvin')
  297. 10.times do
  298. @client.patch_state(
  299. { commands: ['level_down', 'temperature_down'] },
  300. id
  301. )
  302. end
  303. state = @client.get_state(id)
  304. expect(state).to include('level', 'kelvin')
  305. expect(state['level']).to eq(0)
  306. expect(state['kelvin']).to eq(0)
  307. end
  308. it 'should assume state after sufficiently many up commands' do
  309. id = @id_params.merge(type: 'cct')
  310. @client.delete_state(id)
  311. @client.patch_state({status: 'on'}, id)
  312. expect(@client.get_state(id)).to_not include('level', 'kelvin')
  313. 10.times do
  314. @client.patch_state(
  315. { commands: ['level_up', 'temperature_up'] },
  316. id
  317. )
  318. end
  319. state = @client.get_state(id)
  320. expect(state).to include('level', 'kelvin')
  321. expect(state['level']).to eq(100)
  322. expect(state['kelvin']).to eq(100)
  323. end
  324. it 'should affect known state' do
  325. id = @id_params.merge(type: 'cct')
  326. @client.delete_state(id)
  327. @client.patch_state({status: 'on'}, id)
  328. expect(@client.get_state(id)).to_not include('level', 'kelvin')
  329. 10.times do
  330. @client.patch_state(
  331. { commands: ['level_up', 'temperature_up'] },
  332. id
  333. )
  334. end
  335. @client.patch_state(
  336. { commands: ['level_down', 'temperature_down'] },
  337. id
  338. )
  339. state = @client.get_state(id)
  340. expect(state).to include('level', 'kelvin')
  341. expect(state['level']).to eq(90)
  342. expect(state['kelvin']).to eq(90)
  343. end
  344. end
  345. context 'state updates while off' do
  346. it 'should not affect persisted state' do
  347. @client.patch_state({'status' => 'OFF'}, @id_params)
  348. state = @client.patch_state({'hue' => 100}, @id_params)
  349. expect(state.count).to eq(1)
  350. expect(state).to include('status')
  351. end
  352. it 'should not affect persisted state using increment/decrement' do
  353. @client.patch_state({'status' => 'OFF'}, @id_params)
  354. 10.times do
  355. @client.patch_state(
  356. { commands: ['level_down', 'temperature_down'] },
  357. @id_params
  358. )
  359. end
  360. state = @client.get_state(@id_params)
  361. expect(state.count).to eq(1)
  362. expect(state).to include('status')
  363. end
  364. end
  365. context 'fut089' do
  366. # FUT089 uses the same command ID for both kelvin and saturation command, so
  367. # interpreting such a command depends on knowledge of the state that the bulb
  368. # is in.
  369. it 'should keep enough group 0 state to interpret ambiguous kelvin/saturation commands as saturation commands when in color mode' do
  370. group0_params = @id_params.merge(type: 'fut089', group_id: 0)
  371. (0..8).each do |group_id|
  372. @client.delete_state(group0_params.merge(group_id: group_id))
  373. end
  374. # Patch in separate commands so state must be kept
  375. @client.patch_state({'status' => 'ON', 'hue' => 0}, group0_params)
  376. @client.patch_state({'saturation' => 100}, group0_params)
  377. (0..8).each do |group_id|
  378. state = @client.get_state(group0_params.merge(group_id: group_id))
  379. expect(state['bulb_mode']).to eq('color')
  380. expect(state['saturation']).to eq(100)
  381. expect(state['hue']).to eq(0)
  382. end
  383. end
  384. end
  385. context 'fut020' do
  386. it 'should support fut020 commands' do
  387. id = @id_params.merge(type: 'fut020', group_id: 0)
  388. @client.delete_state(id)
  389. state = @client.patch_state({status: 'ON'}, id)
  390. expect(state['status']).to eq('ON')
  391. end
  392. it 'should assume the "off" command sets state to on... commands are the same' do
  393. id = @id_params.merge(type: 'fut020', group_id: 0)
  394. @client.delete_state(id)
  395. state = @client.patch_state({status: 'OFF'}, id)
  396. expect(state['status']).to eq('ON')
  397. end
  398. end
  399. end